-
Notifications
You must be signed in to change notification settings - Fork 0
/
gtk_example.c
277 lines (232 loc) · 10.4 KB
/
gtk_example.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include <gtk/gtk.h>
#include <curl/curl.h>
#include <string.h>
#include <json-glib/json-glib.h>
#include <stdio.h>
#define MAX_FAVORITES 10
#define FAVORITES_FILE "favorites.txt"
struct MemoryStruct {
char *memory;
size_t size;
};
typedef struct {
char cities[MAX_FAVORITES][256];
int count;
} Favorites;
static Favorites favorites;
static GtkWidget *weather_label;
static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) {
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)userp;
char *ptr = realloc(mem->memory, mem->size + realsize + 1);
if(!ptr) {
printf("not enough memory (realloc returned NULL)\n");
return 0;
}
mem->memory = ptr;
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
static void fetch_weather(GtkWidget *widget, gpointer user_data) {
printf("fetch_weather called\n");
GtkEntry *city_entry = GTK_ENTRY(g_object_get_data(G_OBJECT(widget), "city_entry"));
const gchar *city = gtk_entry_get_text(city_entry);
CURL *curl_handle;
CURLcode res;
struct MemoryStruct chunk;
chunk.memory = malloc(1);
chunk.size = 0;
curl_global_init(CURL_GLOBAL_ALL);
curl_handle = curl_easy_init();
if(curl_handle) {
char url[256];
snprintf(url, sizeof(url), "http://api.openweathermap.org/data/2.5/weather?q=%s&appid=Your_API_KEY&units=metric", city);
printf("URL: %s\n", url);
curl_easy_setopt(curl_handle, CURLOPT_URL, url);
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
res = curl_easy_perform(curl_handle);
if(res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
if (GTK_IS_LABEL(weather_label)) {
gtk_label_set_text(GTK_LABEL(weather_label), "Ошибка при получении погоды");
while (g_main_context_pending(NULL)) {
g_main_context_iteration(NULL, FALSE);
}
}
} else {
printf("Data received:\n%s\n", chunk.memory);
JsonParser *parser = json_parser_new();
GError *error = NULL;
if (!json_parser_load_from_data(parser, chunk.memory, -1, &error)) {
g_warning("Error parsing JSON: %s", error->message);
g_error_free(error);
if (GTK_IS_LABEL(weather_label)) {
gtk_label_set_text(GTK_LABEL(weather_label), "Ошибка при разборе JSON");
while (g_main_context_pending(NULL)) {
g_main_context_iteration(NULL, FALSE);
}
}
} else {
JsonNode *root = json_parser_get_root(parser);
JsonObject *object = json_node_get_object(root);
if (object) {
JsonObject *main = json_object_get_object_member(object, "main");
double temp = json_object_get_double_member(main, "temp");
JsonArray *weather_array = json_object_get_array_member(object, "weather");
JsonObject *weather = json_array_get_object_element(weather_array, 0);
const char *description = json_object_get_string_member(weather, "description");
char weather_text[256];
snprintf(weather_text, sizeof(weather_text), "Погода в %s: %.1f°C, %s", city, temp, description);
printf("Weather text: %s\n", weather_text);
if (GTK_IS_LABEL(weather_label)) {
gtk_label_set_text(GTK_LABEL(weather_label), weather_text);
while (g_main_context_pending(NULL)) {
g_main_context_iteration(NULL, FALSE);
}
}
} else {
if (GTK_IS_LABEL(weather_label)) {
gtk_label_set_text(GTK_LABEL(weather_label), "Ошибка: некорректный JSON");
while (g_main_context_pending(NULL)) {
g_main_context_iteration(NULL, FALSE);
}
}
}
}
g_object_unref(parser);
}
curl_easy_cleanup(curl_handle);
free(chunk.memory);
}
curl_global_cleanup();
}
static void save_favorites() {
FILE *file = fopen(FAVORITES_FILE, "w");
if (file) {
for (int i = 0; i < favorites.count; i++) {
fprintf(file, "%s\n", favorites.cities[i]);
}
fclose(file);
}
}
static void load_favorites() {
FILE *file = fopen(FAVORITES_FILE, "r");
if (file) {
favorites.count = 0;
while (fgets(favorites.cities[favorites.count], sizeof(favorites.cities[0]), file) && favorites.count < MAX_FAVORITES) {
favorites.cities[favorites.count][strcspn(favorites.cities[favorites.count], "\n")] = 0;
favorites.count++;
}
fclose(file);
}
}
static void add_favorite(GtkWidget *widget, gpointer user_data) {
GtkEntry *city_entry = GTK_ENTRY(user_data);
const gchar *city = gtk_entry_get_text(city_entry);
if (favorites.count < MAX_FAVORITES && strlen(city) > 0) {
strncpy(favorites.cities[favorites.count], city, sizeof(favorites.cities[0]) - 1);
favorites.count++;
save_favorites();
GtkComboBoxText *combo = GTK_COMBO_BOX_TEXT(g_object_get_data(G_OBJECT(widget), "favorites_combo"));
gtk_combo_box_text_append_text(combo, city);
}
}
static void remove_favorite(GtkWidget *widget, gpointer user_data) {
GtkComboBoxText *combo = GTK_COMBO_BOX_TEXT(user_data);
gchar *selected_city = gtk_combo_box_text_get_active_text(combo);
if (selected_city) {
for (int i = 0; i < favorites.count; i++) {
if (strcmp(favorites.cities[i], selected_city) == 0) {
for (int j = i; j < favorites.count - 1; j++) {
strcpy(favorites.cities[j], favorites.cities[j+1]);
}
favorites.count--;
save_favorites();
gtk_combo_box_text_remove_all(combo);
for (int k = 0; k < favorites.count; k++) {
gtk_combo_box_text_append_text(combo, favorites.cities[k]);
}
break;
}
}
g_free(selected_city);
}
}
static void on_favorite_selected(GtkComboBox *widget, gpointer user_data) {
gchar *selected_city = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(widget));
if (selected_city) {
GtkEntry *city_entry = GTK_ENTRY(user_data);
gtk_entry_set_text(city_entry, selected_city);
g_free(selected_city);
}
}
static void activate(GtkApplication* app, gpointer user_data) {
GtkWidget *window;
GtkWidget *grid;
GtkWidget *button;
GtkWidget *label;
GtkWidget *city_entry;
GtkWidget *favorites_combo;
GtkWidget *add_button;
GtkWidget *remove_button;
window = gtk_application_window_new(app);
gtk_window_set_title(GTK_WINDOW(window), "Погодное приложение");
gtk_window_set_default_size(GTK_WINDOW(window), 400, 300);
grid = gtk_grid_new();
gtk_container_add(GTK_CONTAINER(window), grid);
label = gtk_label_new("Введите название города:");
gtk_grid_attach(GTK_GRID(grid), label, 0, 0, 2, 1);
city_entry = gtk_entry_new();
gtk_entry_set_placeholder_text(GTK_ENTRY(city_entry), "Например: Москва");
gtk_grid_attach(GTK_GRID(grid), city_entry, 0, 1, 2, 1);
favorites_combo = gtk_combo_box_text_new();
gtk_grid_attach(GTK_GRID(grid), favorites_combo, 0, 2, 2, 1);
add_button = gtk_button_new_with_label("Добавить в избранное");
gtk_grid_attach(GTK_GRID(grid), add_button, 0, 3, 1, 1);
gtk_widget_set_hexpand(add_button, TRUE);
gtk_widget_set_vexpand(add_button, TRUE);
gtk_widget_set_halign(add_button, GTK_ALIGN_FILL);
gtk_widget_set_valign(add_button, GTK_ALIGN_FILL);
remove_button = gtk_button_new_with_label("Удалить из избранного");
gtk_grid_attach(GTK_GRID(grid), remove_button, 1, 3, 1, 1);
gtk_widget_set_hexpand(remove_button, TRUE);
gtk_widget_set_vexpand(remove_button, TRUE);
gtk_widget_set_halign(remove_button, GTK_ALIGN_FILL);
gtk_widget_set_valign(remove_button, GTK_ALIGN_FILL);
button = gtk_button_new_with_label("Узнать погоду");
g_object_set_data(G_OBJECT(button), "city_entry", city_entry);
gtk_grid_attach(GTK_GRID(grid), button, 0, 4, 2, 1);
gtk_widget_set_hexpand(button, TRUE);
gtk_widget_set_vexpand(button, TRUE);
gtk_widget_set_halign(button, GTK_ALIGN_FILL);
gtk_widget_set_valign(button, GTK_ALIGN_FILL);
weather_label = gtk_label_new("Здесь появится информация о погоде");
gtk_grid_attach(GTK_GRID(grid), weather_label, 0, 5, 2, 1);
gtk_widget_set_hexpand(weather_label, TRUE);
gtk_widget_set_vexpand(weather_label, TRUE);
gtk_widget_set_halign(weather_label, GTK_ALIGN_FILL);
gtk_widget_set_valign(weather_label, GTK_ALIGN_FILL);
g_signal_connect(button, "clicked", G_CALLBACK(fetch_weather), NULL);
g_signal_connect(add_button, "clicked", G_CALLBACK(add_favorite), city_entry);
g_object_set_data(G_OBJECT(add_button), "favorites_combo", favorites_combo);
g_signal_connect(remove_button, "clicked", G_CALLBACK(remove_favorite), favorites_combo);
g_signal_connect(favorites_combo, "changed", G_CALLBACK(on_favorite_selected), city_entry);
load_favorites();
for (int i = 0; i < favorites.count; i++) {
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(favorites_combo), favorites.cities[i]);
}
gtk_widget_show_all(window);
}
int main(int argc, char **argv) {
GtkApplication *app;
int status;
app = gtk_application_new("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
return status;
}