diff options
| author | Simeon Simeonov | 2018-02-26 11:23:00 +0100 |
|---|---|---|
| committer | Simeon Simeonov | 2018-02-26 11:23:00 +0100 |
| commit | 0b3cbf57875fd692e4ba0b336fefa4bee1ed00dc (patch) | |
| tree | af916a30553c78ce9d4f2d8658656a175c5b6921 /src/update_check.c | |
Initial commit for sylpheed 3.7.0
Diffstat (limited to 'src/update_check.c')
| -rw-r--r-- | src/update_check.c | 826 |
1 files changed, 826 insertions, 0 deletions
diff --git a/src/update_check.c b/src/update_check.c new file mode 100644 index 0000000..d677ebb --- /dev/null +++ b/src/update_check.c | |||
| @@ -0,0 +1,826 @@ | |||
| 1 | /* | ||
| 2 | * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client | ||
| 3 | * Copyright (C) 1999-2015 Hiroyuki Yamamoto | ||
| 4 | * | ||
| 5 | * This program is free software; you can redistribute it and/or modify | ||
| 6 | * it under the terms of the GNU General Public License as published by | ||
| 7 | * the Free Software Foundation; either version 2 of the License, or | ||
| 8 | * (at your option) any later version. | ||
| 9 | * | ||
| 10 | * This program is distributed in the hope that it will be useful, | ||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | * GNU General Public License for more details. | ||
| 14 | * | ||
| 15 | * You should have received a copy of the GNU General Public License | ||
| 16 | * along with this program; if not, write to the Free Software | ||
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
| 18 | */ | ||
| 19 | |||
| 20 | #ifdef HAVE_CONFIG_H | ||
| 21 | # include "config.h" | ||
| 22 | #endif | ||
| 23 | |||
| 24 | #ifdef USE_UPDATE_CHECK | ||
| 25 | |||
| 26 | #include "defs.h" | ||
| 27 | |||
| 28 | #include <glib.h> | ||
| 29 | #include <glib/gi18n.h> | ||
| 30 | |||
| 31 | #include <gdk/gdkkeysyms.h> | ||
| 32 | #include <gtk/gtk.h> | ||
| 33 | |||
| 34 | #include <stdlib.h> | ||
| 35 | #include <unistd.h> | ||
| 36 | |||
| 37 | #include "update_check.h" | ||
| 38 | #include "manage_window.h" | ||
| 39 | #include "inc.h" | ||
| 40 | #include "gtkutils.h" | ||
| 41 | #include "alertpanel.h" | ||
| 42 | #include "prefs_common.h" | ||
| 43 | #include "socket.h" | ||
| 44 | #include "utils.h" | ||
| 45 | #include "version.h" | ||
| 46 | #include "plugin.h" | ||
| 47 | |||
| 48 | static gchar *check_url = NULL; | ||
| 49 | static gchar *download_url = NULL; | ||
| 50 | static gchar *jump_url = NULL; | ||
| 51 | #ifdef USE_UPDATE_CHECK_PLUGIN | ||
| 52 | static gchar *check_plugin_url = NULL; | ||
| 53 | static gchar *jump_plugin_url = NULL; | ||
| 54 | #endif /* USE_UPDATE_CHECK_PLUGIN */ | ||
| 55 | |||
| 56 | static gint compare_version(gint lmajor, gint lminor, gint lmicro, | ||
| 57 | gint rmajor, gint rminor, gint rmicro) | ||
| 58 | { | ||
| 59 | debug_print("comparing %d.%d.%d <> %d.%d.%d\n", | ||
| 60 | lmajor, lminor, lmicro, | ||
| 61 | rmajor, rminor, rmicro); | ||
| 62 | |||
| 63 | if (lmajor > rmajor) | ||
| 64 | return 1; | ||
| 65 | if (lmajor < rmajor) | ||
| 66 | return -1; | ||
| 67 | if (lminor > rminor) | ||
| 68 | return 1; | ||
| 69 | if (lminor < rminor) | ||
| 70 | return -1; | ||
| 71 | if (lmicro > rmicro) | ||
| 72 | return 1; | ||
| 73 | if (lmicro < rmicro) | ||
| 74 | return -1; | ||
| 75 | |||
| 76 | return 0; | ||
| 77 | } | ||
| 78 | |||
| 79 | static gboolean compare_sylpheed_version(gint major, gint minor, gint micro, | ||
| 80 | const gchar *extra, gboolean remote_is_release, | ||
| 81 | gboolean cur_ver_is_release) | ||
| 82 | { | ||
| 83 | debug_print("comparing %d.%d.%d.%s (%s) <> " VERSION " (%s)\n", | ||
| 84 | major, minor, micro, extra ? extra : "", | ||
| 85 | remote_is_release ? "release" : "devel", | ||
| 86 | cur_ver_is_release ? "release" : "devel"); | ||
| 87 | |||
| 88 | switch (compare_version(major, minor, micro, | ||
| 89 | MAJOR_VERSION, MINOR_VERSION, MICRO_VERSION)) { | ||
| 90 | case 1: | ||
| 91 | return TRUE; | ||
| 92 | case -1: | ||
| 93 | return FALSE; | ||
| 94 | default: | ||
| 95 | break; | ||
| 96 | } | ||
| 97 | |||
| 98 | /* compare extra version | ||
| 99 | 3.0.0.a (rel) > 3.0.0 (rel) | ||
| 100 | 3.0.0 (rel) > 3.0.0.betaX (dev) | ||
| 101 | 3.0.0.a (rel) > 3.0.0.betaX (dev) | ||
| 102 | 3.0.0.rc (dev) > 3.0.0.betaX (dev) */ | ||
| 103 | if (remote_is_release && !cur_ver_is_release) | ||
| 104 | return TRUE; | ||
| 105 | if (!remote_is_release && cur_ver_is_release) | ||
| 106 | return FALSE; | ||
| 107 | |||
| 108 | if (extra) { | ||
| 109 | if (strcmp(extra, EXTRA_VERSION) > 0) | ||
| 110 | return TRUE; | ||
| 111 | } | ||
| 112 | |||
| 113 | return FALSE; | ||
| 114 | } | ||
| 115 | |||
| 116 | static void parse_version_string(const gchar *ver, gint *major, gint *minor, | ||
| 117 | gint *micro, gchar **extra) | ||
| 118 | { | ||
| 119 | gchar **vers; | ||
| 120 | |||
| 121 | vers = g_strsplit(ver, ".", -1); | ||
| 122 | if (vers[0]) { | ||
| 123 | *major = atoi(vers[0]); | ||
| 124 | if (vers[1]) { | ||
| 125 | *minor = atoi(vers[1]); | ||
| 126 | if (vers[2]) { | ||
| 127 | *micro = atoi(vers[2]); | ||
| 128 | if (vers[3] && extra) { | ||
| 129 | *extra = g_strdup(vers[3]); | ||
| 130 | } | ||
| 131 | } | ||
| 132 | } | ||
| 133 | } | ||
| 134 | g_strfreev(vers); | ||
| 135 | } | ||
| 136 | |||
| 137 | #ifdef G_OS_WIN32 | ||
| 138 | static gboolean spawn_update_manager(void) | ||
| 139 | { | ||
| 140 | gchar *src = NULL, *dest = NULL, *quoted_uri = NULL; | ||
| 141 | gchar *cmdline[] = {NULL, "/uri", NULL, NULL}; | ||
| 142 | GError *error = NULL; | ||
| 143 | gboolean ret = FALSE; | ||
| 144 | |||
| 145 | src = g_strconcat(get_startup_dir(), G_DIR_SEPARATOR_S, "update-manager.exe", NULL); | ||
| 146 | if (!is_file_exist(src) || get_file_size(src) <= 0) { | ||
| 147 | g_warning("update-manager.exe not found."); | ||
| 148 | goto finish; | ||
| 149 | } | ||
| 150 | dest = g_strconcat(g_get_tmp_dir(), G_DIR_SEPARATOR_S, "sylpheed-update-manager.exe", NULL); | ||
| 151 | if (copy_file(src, dest, FALSE) < 0) { | ||
| 152 | g_warning("Couldn't copy update-manager.exe"); | ||
| 153 | goto finish; | ||
| 154 | } | ||
| 155 | quoted_uri = g_strdup_printf("'%s'", download_url); | ||
| 156 | cmdline[0] = dest; | ||
| 157 | cmdline[2] = quoted_uri; | ||
| 158 | if (g_spawn_async | ||
| 159 | (NULL, cmdline, NULL, | ||
| 160 | G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL, | ||
| 161 | NULL, NULL, NULL, &error) == FALSE) { | ||
| 162 | g_warning("Couldn't execute update-manager.exe"); | ||
| 163 | if (error) { | ||
| 164 | g_warning("g_spawn_async: %s", error->message); | ||
| 165 | g_error_free(error); | ||
| 166 | } | ||
| 167 | } else { | ||
| 168 | ret = TRUE; | ||
| 169 | } | ||
| 170 | finish: | ||
| 171 | g_free(src); | ||
| 172 | g_free(dest); | ||
| 173 | g_free(quoted_uri); | ||
| 174 | return ret; | ||
| 175 | } | ||
| 176 | |||
| 177 | #ifdef USE_UPDATE_CHECK_PLUGIN | ||
| 178 | static gchar *plugin_updater_ini = NULL; | ||
| 179 | |||
| 180 | void update_check_spawn_plugin_updater(void) | ||
| 181 | { | ||
| 182 | gchar *exe = NULL, *quoted_ini = NULL; | ||
| 183 | gchar *cmdline[] = {NULL, "/ini", NULL, NULL}; | ||
| 184 | GError *error = NULL; | ||
| 185 | gboolean ret = FALSE; | ||
| 186 | |||
| 187 | if (!plugin_updater_ini) | ||
| 188 | return; | ||
| 189 | if (!is_file_exist(plugin_updater_ini)) { | ||
| 190 | g_warning("Not found %s", plugin_updater_ini); | ||
| 191 | goto finish; | ||
| 192 | } | ||
| 193 | exe = g_strconcat(get_startup_dir(), G_DIR_SEPARATOR_S, "plugin-updater.exe", NULL); | ||
| 194 | if (!is_file_exist(exe) || get_file_size(exe) <= 0) { | ||
| 195 | g_warning("Not found plugin-updater.exe"); | ||
| 196 | goto finish; | ||
| 197 | } | ||
| 198 | |||
| 199 | quoted_ini = g_strdup_printf("'%s'", plugin_updater_ini); | ||
| 200 | cmdline[0] = exe; | ||
| 201 | cmdline[2] = quoted_ini; | ||
| 202 | if (g_spawn_sync | ||
| 203 | (NULL, cmdline, NULL, | ||
| 204 | G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL, | ||
| 205 | NULL, NULL, NULL, NULL, NULL, &error) == FALSE) { | ||
| 206 | g_warning("Couldn't execute plugin-updater.exe"); | ||
| 207 | debug_print("Couldn't execute plugin-updater.exe\n"); | ||
| 208 | if (error) { | ||
| 209 | g_warning("g_spawn_async: %s", error->message); | ||
| 210 | debug_print("g_spawn_async: %s\n", error->message); | ||
| 211 | g_error_free(error); | ||
| 212 | } | ||
| 213 | } else { | ||
| 214 | ret = TRUE; | ||
| 215 | } | ||
| 216 | finish: | ||
| 217 | g_free(exe); | ||
| 218 | g_free(quoted_ini); | ||
| 219 | g_free(plugin_updater_ini); | ||
| 220 | plugin_updater_ini = NULL; | ||
| 221 | } | ||
| 222 | #endif /* USE_UPDATE_CHECK_PLUGIN */ | ||
| 223 | #endif /* G_OS_WIN32 */ | ||
| 224 | |||
| 225 | #ifdef G_OS_WIN32 | ||
| 226 | static void set_default_download_url(void) | ||
| 227 | { | ||
| 228 | gchar buf[1024]; | ||
| 229 | const gchar *os; | ||
| 230 | |||
| 231 | #ifdef G_OS_WIN32 | ||
| 232 | os = "win"; | ||
| 233 | #else | ||
| 234 | if (strstr(TARGET_ALIAS, "linux")) | ||
| 235 | os = "linux"; | ||
| 236 | else | ||
| 237 | os = "other"; | ||
| 238 | #endif | ||
| 239 | |||
| 240 | #ifdef DEVEL_VERSION | ||
| 241 | g_snprintf(buf, sizeof(buf), "%s?ver=%s&os=%s&dev=t", | ||
| 242 | DOWNLOAD_URI, VERSION, os); | ||
| 243 | #else | ||
| 244 | g_snprintf(buf, sizeof(buf), "%s?ver=%s&os=%s", | ||
| 245 | DOWNLOAD_URI, VERSION, os); | ||
| 246 | #endif | ||
| 247 | update_check_set_download_url(buf); | ||
| 248 | } | ||
| 249 | #endif | ||
| 250 | |||
| 251 | static void update_dialog(const gchar *new_ver, const gchar *disp_ver, | ||
| 252 | gboolean manual) | ||
| 253 | { | ||
| 254 | gchar buf[1024]; | ||
| 255 | AlertValue val; | ||
| 256 | |||
| 257 | if (!jump_url) | ||
| 258 | update_check_set_jump_url(HOMEPAGE_URI); | ||
| 259 | |||
| 260 | if (new_ver) { | ||
| 261 | if (disp_ver) | ||
| 262 | g_snprintf(buf, sizeof(buf), "%s\n\n%s\n(%s -> %s)", | ||
| 263 | _("A newer version of Sylpheed has been found.\n" | ||
| 264 | "Upgrade now?"), | ||
| 265 | disp_ver, VERSION, new_ver); | ||
| 266 | else | ||
| 267 | g_snprintf(buf, sizeof(buf), "%s\n\n%s -> %s", | ||
| 268 | _("A newer version of Sylpheed has been found.\n" | ||
| 269 | "Upgrade now?"), | ||
| 270 | VERSION, new_ver); | ||
| 271 | } else | ||
| 272 | g_snprintf(buf, sizeof(buf), "%s", | ||
| 273 | _("A newer version of Sylpheed has been found.\n" | ||
| 274 | "Upgrade now?")); | ||
| 275 | |||
| 276 | val = alertpanel_full(_("New version found"), buf, | ||
| 277 | ALERT_QUESTION, | ||
| 278 | G_ALERTDEFAULT, | ||
| 279 | manual ? FALSE : TRUE, | ||
| 280 | GTK_STOCK_YES, GTK_STOCK_NO, NULL); | ||
| 281 | if ((val & G_ALERT_VALUE_MASK) == G_ALERTDEFAULT) { | ||
| 282 | #ifdef G_OS_WIN32 | ||
| 283 | if (!download_url) | ||
| 284 | set_default_download_url(); | ||
| 285 | if (!spawn_update_manager()) | ||
| 286 | open_uri(jump_url, prefs_common.uri_cmd); | ||
| 287 | #else | ||
| 288 | open_uri(jump_url, prefs_common.uri_cmd); | ||
| 289 | #endif | ||
| 290 | } | ||
| 291 | if (val & G_ALERTDISABLE) { | ||
| 292 | prefs_common.auto_update_check = FALSE; | ||
| 293 | } | ||
| 294 | } | ||
| 295 | |||
| 296 | static gint child_stdout; | ||
| 297 | |||
| 298 | static gchar *read_child_stdout_and_close(gint fd) | ||
| 299 | { | ||
| 300 | GIOChannel *ch; | ||
| 301 | gchar *str = NULL; | ||
| 302 | gsize len = 0; | ||
| 303 | |||
| 304 | ch = g_io_channel_unix_new(fd); | ||
| 305 | g_io_channel_read_to_end(ch, &str, &len, NULL); | ||
| 306 | g_io_channel_shutdown(ch, TRUE, NULL); | ||
| 307 | g_io_channel_unref(ch); | ||
| 308 | |||
| 309 | return str; | ||
| 310 | } | ||
| 311 | |||
| 312 | static void close_child_stdout(gint fd) | ||
| 313 | { | ||
| 314 | #ifdef G_OS_WIN32 | ||
| 315 | GIOChannel *ch; | ||
| 316 | |||
| 317 | ch = g_io_channel_win32_new_fd(fd); | ||
| 318 | g_io_channel_shutdown(ch, TRUE, NULL); | ||
| 319 | g_io_channel_unref(ch); | ||
| 320 | #else | ||
| 321 | fd_close(fd); | ||
| 322 | #endif | ||
| 323 | } | ||
| 324 | |||
| 325 | static void update_check_cb(GPid pid, gint status, gpointer data) | ||
| 326 | { | ||
| 327 | gchar **lines; | ||
| 328 | gchar *key, *val, *p; | ||
| 329 | gchar *new_ver = NULL; | ||
| 330 | gint i; | ||
| 331 | #ifdef DEVEL_VERSION | ||
| 332 | gboolean cur_ver_is_release = FALSE; | ||
| 333 | #else | ||
| 334 | gboolean cur_ver_is_release = TRUE; | ||
| 335 | #endif | ||
| 336 | gboolean result = FALSE; | ||
| 337 | gboolean got_version = FALSE; | ||
| 338 | gboolean rel_result = FALSE; | ||
| 339 | gboolean dev_result = FALSE; | ||
| 340 | gboolean show_dialog_always = GPOINTER_TO_INT(data); | ||
| 341 | gchar *str; | ||
| 342 | gchar *disp_rel_ver = NULL; | ||
| 343 | gchar *disp_dev_ver = NULL; | ||
| 344 | |||
| 345 | debug_print("update_check_cb\n"); | ||
| 346 | |||
| 347 | if (!child_stdout) { | ||
| 348 | g_spawn_close_pid(pid); | ||
| 349 | return; | ||
| 350 | } | ||
| 351 | |||
| 352 | str = read_child_stdout_and_close(child_stdout); | ||
| 353 | child_stdout = 0; | ||
| 354 | g_spawn_close_pid(pid); | ||
| 355 | |||
| 356 | if (!str) { | ||
| 357 | return; | ||
| 358 | } | ||
| 359 | |||
| 360 | lines = g_strsplit(str, "\n", -1); | ||
| 361 | |||
| 362 | for (i = 0; lines[i] != NULL; i++) { | ||
| 363 | gint major = 0, minor = 0, micro = 0; | ||
| 364 | gchar *extra = NULL; | ||
| 365 | |||
| 366 | debug_print("update_check: %s\n", lines[i]); | ||
| 367 | p = strchr(lines[i], '='); | ||
| 368 | if (!p) continue; | ||
| 369 | key = g_strndup(lines[i], p - lines[i]); | ||
| 370 | val = p + 1; | ||
| 371 | |||
| 372 | if (!disp_rel_ver && !strcmp(key, "DISP_RELEASE")) { | ||
| 373 | disp_rel_ver = g_strdup(val); | ||
| 374 | } else if (!cur_ver_is_release && !disp_dev_ver && | ||
| 375 | !strcmp(key, "DISP_DEVEL")) { | ||
| 376 | disp_dev_ver = g_strdup(val); | ||
| 377 | } | ||
| 378 | |||
| 379 | if (!result) { | ||
| 380 | if (!strcmp(key, "RELEASE")) { | ||
| 381 | parse_version_string(val, &major, &minor, µ, | ||
| 382 | &extra); | ||
| 383 | result = compare_sylpheed_version(major, minor, micro, extra, | ||
| 384 | TRUE, cur_ver_is_release); | ||
| 385 | rel_result = result; | ||
| 386 | } else if (!cur_ver_is_release && !strcmp(key, "DEVEL")) { | ||
| 387 | parse_version_string(val, &major, &minor, µ, | ||
| 388 | &extra); | ||
| 389 | result = compare_sylpheed_version(major, minor, micro, extra, | ||
| 390 | FALSE, cur_ver_is_release); | ||
| 391 | dev_result = result; | ||
| 392 | } | ||
| 393 | |||
| 394 | if (major + minor + micro != 0) | ||
| 395 | got_version = TRUE; | ||
| 396 | |||
| 397 | if (result) { | ||
| 398 | new_ver = g_strdup_printf("%d.%d.%d%s", major, minor, micro, extra ? extra : ""); | ||
| 399 | debug_print("update_check: new ver: %s (%s)\n", new_ver, rel_result ? "release version" : "devel version"); | ||
| 400 | } | ||
| 401 | } | ||
| 402 | |||
| 403 | g_free(extra); | ||
| 404 | g_free(key); | ||
| 405 | } | ||
| 406 | |||
| 407 | g_strfreev(lines); | ||
| 408 | g_free(str); | ||
| 409 | |||
| 410 | gdk_threads_enter(); | ||
| 411 | |||
| 412 | if (!gtkut_window_modal_exist() && !inc_is_active()) { | ||
| 413 | if (result) { | ||
| 414 | if (rel_result) | ||
| 415 | update_dialog(new_ver, disp_rel_ver, show_dialog_always); | ||
| 416 | else | ||
| 417 | update_dialog(new_ver, disp_dev_ver, show_dialog_always); | ||
| 418 | } else if (show_dialog_always) { | ||
| 419 | if (got_version) | ||
| 420 | alertpanel_message(_("Information"), | ||
| 421 | _("Sylpheed is already the latest version."), | ||
| 422 | ALERT_NOTICE); | ||
| 423 | else | ||
| 424 | alertpanel_error(_("Couldn't get the version information.")); | ||
| 425 | } | ||
| 426 | } else { | ||
| 427 | debug_print("update_check_cb: modal dialog exists or incorporation is active. Disabling update dialog.\n"); | ||
| 428 | } | ||
| 429 | |||
| 430 | g_free(disp_rel_ver); | ||
| 431 | g_free(disp_dev_ver); | ||
| 432 | g_free(new_ver); | ||
| 433 | |||
| 434 | gdk_threads_leave(); | ||
| 435 | } | ||
| 436 | |||
| 437 | static void spawn_curl(gchar *url, GChildWatchFunc func, gpointer data) | ||
| 438 | { | ||
| 439 | gchar *cmdline[8] = {"curl", "--silent", "--max-time", "10"}; | ||
| 440 | gint argc = 4; | ||
| 441 | GPid pid; | ||
| 442 | GError *error = NULL; | ||
| 443 | |||
| 444 | if (child_stdout > 0) { | ||
| 445 | debug_print("update check is in progress\n"); | ||
| 446 | return; | ||
| 447 | } | ||
| 448 | |||
| 449 | child_stdout = 0; | ||
| 450 | |||
| 451 | debug_print("spawn_curl: getting from %s\n", url); | ||
| 452 | |||
| 453 | cmdline[argc++] = url; | ||
| 454 | if (prefs_common.use_http_proxy && prefs_common.http_proxy_host && | ||
| 455 | prefs_common.http_proxy_host[0] != '\0') { | ||
| 456 | cmdline[argc++] = "--proxy"; | ||
| 457 | cmdline[argc++] = prefs_common.http_proxy_host; | ||
| 458 | } | ||
| 459 | cmdline[argc++] = NULL; | ||
| 460 | |||
| 461 | if (g_spawn_async_with_pipes | ||
| 462 | (NULL, cmdline, NULL, | ||
| 463 | G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH, | ||
| 464 | NULL, NULL, &pid, | ||
| 465 | NULL, &child_stdout, NULL, &error) == FALSE) { | ||
| 466 | g_warning("Couldn't execute curl"); | ||
| 467 | if (error) { | ||
| 468 | g_warning("g_spawn_async_with_pipes: %s", | ||
| 469 | error->message); | ||
| 470 | g_error_free(error); | ||
| 471 | } | ||
| 472 | return; | ||
| 473 | } | ||
| 474 | if (pid == 0) { | ||
| 475 | g_warning("Couldn't get PID of child process"); | ||
| 476 | if (child_stdout) { | ||
| 477 | close_child_stdout(child_stdout); | ||
| 478 | child_stdout = 0; | ||
| 479 | } | ||
| 480 | return; | ||
| 481 | } | ||
| 482 | |||
| 483 | g_child_watch_add(pid, func, data); | ||
| 484 | } | ||
| 485 | |||
| 486 | #ifdef USE_UPDATE_CHECK_PLUGIN | ||
| 487 | struct download_plugin_info { | ||
| 488 | const gchar *filename; | ||
| 489 | const SylPluginInfo* info; | ||
| 490 | gchar *url; | ||
| 491 | gint major, minor, micro; | ||
| 492 | }; | ||
| 493 | |||
| 494 | static void download_plugin_info_free(struct download_plugin_info *pinfo) | ||
| 495 | { | ||
| 496 | if (!pinfo) | ||
| 497 | return; | ||
| 498 | g_free(pinfo->url); | ||
| 499 | g_free(pinfo); | ||
| 500 | } | ||
| 501 | |||
| 502 | static GHashTable *get_plugin_version_table(void) | ||
| 503 | { | ||
| 504 | GSList *list, *cur; | ||
| 505 | SylPluginInfo *info; | ||
| 506 | GModule *module; | ||
| 507 | struct download_plugin_info *pinfo; | ||
| 508 | GHashTable *plugin_version_table = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, (GDestroyNotify)download_plugin_info_free); | ||
| 509 | |||
| 510 | list = syl_plugin_get_module_list(); | ||
| 511 | for (cur = list; cur != NULL; cur = cur->next) { | ||
| 512 | module = (GModule *)cur->data; | ||
| 513 | |||
| 514 | info = syl_plugin_get_info(module); | ||
| 515 | if (info) { | ||
| 516 | pinfo = g_new0(struct download_plugin_info, 1); | ||
| 517 | pinfo->filename = g_module_name(module); | ||
| 518 | pinfo->info = info; | ||
| 519 | g_hash_table_insert(plugin_version_table, info->name, pinfo); | ||
| 520 | } else { | ||
| 521 | debug_print("info not found: %s\n", g_module_name(module)); | ||
| 522 | } | ||
| 523 | } | ||
| 524 | |||
| 525 | return plugin_version_table; | ||
| 526 | } | ||
| 527 | |||
| 528 | #ifdef G_OS_WIN32 | ||
| 529 | static gboolean write_plugin_updater_ini(GSList *list) | ||
| 530 | { | ||
| 531 | guint num, h = 12; | ||
| 532 | struct download_plugin_info *pinfo; | ||
| 533 | GString *ini = g_string_new("[Settings]\n"); | ||
| 534 | GSList *cur; | ||
| 535 | gchar *basename, *p; | ||
| 536 | gboolean ret = TRUE; | ||
| 537 | |||
| 538 | num = g_slist_length(list); | ||
| 539 | g_string_append_printf(ini, "NumFields=%d\n", num+1); | ||
| 540 | |||
| 541 | num = 0; | ||
| 542 | g_string_append_printf(ini, "\n[Field %d]\n", ++num); | ||
| 543 | g_string_append_printf(ini, "Type=GroupBox\n"); | ||
| 544 | g_string_append_printf(ini, "Left=0\n"); | ||
| 545 | g_string_append_printf(ini, "Right=-1\n"); | ||
| 546 | g_string_append_printf(ini, "Top=0\n"); | ||
| 547 | g_string_append_printf(ini, "Bottom=-5\n"); | ||
| 548 | g_string_append_printf(ini, "Text=\" Select update plugins \"\n"); | ||
| 549 | |||
| 550 | for (cur = list; cur != NULL; cur = cur->next) { | ||
| 551 | pinfo = cur->data; | ||
| 552 | g_string_append_printf(ini, "\n[Field %d]\n", ++num); | ||
| 553 | g_string_append_printf(ini, "Type=checkbox\n"); | ||
| 554 | g_string_append_printf(ini, "Text=%s %s -> %d.%d.%d\n", | ||
| 555 | pinfo->info->name, pinfo->info->version, | ||
| 556 | pinfo->major, pinfo->minor, pinfo->micro); | ||
| 557 | g_string_append_printf(ini, "Left=10\n"); | ||
| 558 | g_string_append_printf(ini, "Right=-10\n"); | ||
| 559 | g_string_append_printf(ini, "Top=%u\n", (h+=5)); | ||
| 560 | g_string_append_printf(ini, "Bottom=%u\n", (h+=8)); | ||
| 561 | g_string_append_printf(ini, "State=1\n"); | ||
| 562 | g_string_append_printf(ini, "URL=%s\n", pinfo->url); | ||
| 563 | g_string_append_printf(ini, "name=%s\n", pinfo->info->name); | ||
| 564 | |||
| 565 | basename = g_path_get_basename(pinfo->filename); | ||
| 566 | p = strrchr(basename, '.'); | ||
| 567 | if (p) *p = '\0'; /* cut ".dll" */ | ||
| 568 | g_string_append_printf(ini, "basename=%s\n", basename); | ||
| 569 | g_free(basename); | ||
| 570 | } | ||
| 571 | |||
| 572 | debug_print("write_plugin_updater_ini:\n%s\n", ini->str); | ||
| 573 | |||
| 574 | plugin_updater_ini = g_strconcat(g_get_tmp_dir(), G_DIR_SEPARATOR_S, "sylpheed-plugin-updater.ini", NULL); | ||
| 575 | if (str_write_to_file(ini->str, plugin_updater_ini) < 0) { | ||
| 576 | g_free(plugin_updater_ini); | ||
| 577 | plugin_updater_ini = NULL; | ||
| 578 | ret = FALSE; | ||
| 579 | } | ||
| 580 | |||
| 581 | g_string_free(ini, TRUE); | ||
| 582 | return ret; | ||
| 583 | } | ||
| 584 | #endif /* G_OS_WIN32 */ | ||
| 585 | |||
| 586 | static void update_plugin_dialog(GString *text, GSList *list) | ||
| 587 | { | ||
| 588 | AlertValue val; | ||
| 589 | |||
| 590 | if (!jump_plugin_url) | ||
| 591 | update_check_set_jump_plugin_url(PLUGIN_HOMEPAGE_URI); | ||
| 592 | |||
| 593 | val = alertpanel_full(_("New version found"), text->str, | ||
| 594 | ALERT_QUESTION, | ||
| 595 | G_ALERTDEFAULT, | ||
| 596 | FALSE, | ||
| 597 | GTK_STOCK_YES, GTK_STOCK_NO, NULL); | ||
| 598 | if ((val & G_ALERT_VALUE_MASK) == G_ALERTDEFAULT) { | ||
| 599 | #ifdef G_OS_WIN32 | ||
| 600 | if (write_plugin_updater_ini(list)) | ||
| 601 | app_will_restart(TRUE); | ||
| 602 | #else | ||
| 603 | open_uri(jump_plugin_url, prefs_common.uri_cmd); | ||
| 604 | #endif | ||
| 605 | } | ||
| 606 | } | ||
| 607 | |||
| 608 | static void update_check_plugin_cb(GPid pid, gint status, gpointer data) | ||
| 609 | { | ||
| 610 | gchar **lines; | ||
| 611 | gchar *key, *val, *p; | ||
| 612 | gchar *cur_ver; | ||
| 613 | gint i; | ||
| 614 | gboolean show_dialog_always = GPOINTER_TO_INT(data); | ||
| 615 | gchar *str; | ||
| 616 | GHashTable *plugin_version_table = NULL; | ||
| 617 | struct download_plugin_info *pinfo = NULL; | ||
| 618 | gboolean result = FALSE; | ||
| 619 | gboolean got_version = FALSE; | ||
| 620 | GString *text = NULL; | ||
| 621 | GSList *list = NULL; | ||
| 622 | |||
| 623 | debug_print("update_check_plugin_cb\n"); | ||
| 624 | |||
| 625 | if (!child_stdout) { | ||
| 626 | g_spawn_close_pid(pid); | ||
| 627 | return; | ||
| 628 | } | ||
| 629 | |||
| 630 | str = read_child_stdout_and_close(child_stdout); | ||
| 631 | child_stdout = 0; | ||
| 632 | g_spawn_close_pid(pid); | ||
| 633 | |||
| 634 | if (!str) { | ||
| 635 | return; | ||
| 636 | } | ||
| 637 | |||
| 638 | lines = g_strsplit(str, "\n", -1); | ||
| 639 | plugin_version_table = get_plugin_version_table(); | ||
| 640 | text = g_string_new(_("Newer version of plug-ins have been found.\n" | ||
| 641 | "Upgrade now?\n")); | ||
| 642 | |||
| 643 | for (i = 0; lines[i] != NULL; i++) { | ||
| 644 | gint new_major = 0, new_minor = 0, new_micro = 0; | ||
| 645 | gint cur_major = 0, cur_minor = 0, cur_micro = 0; | ||
| 646 | debug_print("update_check_plugin: %s\n", lines[i]); | ||
| 647 | p = strchr(lines[i], '='); | ||
| 648 | if (!p) continue; | ||
| 649 | key = g_strndup(lines[i], p - lines[i]); | ||
| 650 | val = p + 1; | ||
| 651 | |||
| 652 | parse_version_string(val, &new_major, &new_minor, &new_micro, NULL); | ||
| 653 | if (new_major + new_minor + new_micro != 0) { | ||
| 654 | got_version = TRUE; | ||
| 655 | } | ||
| 656 | |||
| 657 | pinfo = g_hash_table_lookup(plugin_version_table, key); | ||
| 658 | if (pinfo && (cur_ver = pinfo->info->version)) { | ||
| 659 | parse_version_string(cur_ver, &cur_major, &cur_minor, &cur_micro, NULL); | ||
| 660 | if (0 < compare_version(new_major, new_minor, new_micro, | ||
| 661 | cur_major, cur_minor, cur_micro)) { | ||
| 662 | g_string_append_printf(text, "\n - %s %d.%d.%d -> %d.%d.%d", key, | ||
| 663 | cur_major, cur_minor, cur_micro, | ||
| 664 | new_major, new_minor, new_micro); | ||
| 665 | |||
| 666 | result = TRUE; | ||
| 667 | |||
| 668 | debug_print("val = %s\n", val); | ||
| 669 | p = strchr(val, ','); | ||
| 670 | if (p) { | ||
| 671 | struct download_plugin_info *pinfo2 = g_new0(struct download_plugin_info, 1); | ||
| 672 | pinfo2->filename = pinfo->filename; | ||
| 673 | pinfo2->info = pinfo->info; | ||
| 674 | pinfo2->url = g_strdup(p + 1); /* skip ',' */ | ||
| 675 | pinfo2->major = new_major; | ||
| 676 | pinfo2->minor = new_minor; | ||
| 677 | pinfo2->micro = new_micro; | ||
| 678 | list = g_slist_append(list, pinfo2); | ||
| 679 | } | ||
| 680 | } | ||
| 681 | } | ||
| 682 | |||
| 683 | g_free(key); | ||
| 684 | } | ||
| 685 | |||
| 686 | g_hash_table_destroy(plugin_version_table); | ||
| 687 | g_strfreev(lines); | ||
| 688 | g_free(str); | ||
| 689 | |||
| 690 | debug_print("%s\n", text->str); | ||
| 691 | |||
| 692 | gdk_threads_enter(); | ||
| 693 | |||
| 694 | if (!gtkut_window_modal_exist() && !inc_is_active()) { | ||
| 695 | if (result) { | ||
| 696 | update_plugin_dialog(text, list); | ||
| 697 | list = NULL; | ||
| 698 | } else if (show_dialog_always) { | ||
| 699 | if (got_version) | ||
| 700 | alertpanel_message(_("Information"), | ||
| 701 | _("All Sylpheed plug-ins are already the latest version."), | ||
| 702 | ALERT_NOTICE); | ||
| 703 | else | ||
| 704 | alertpanel_error(_("Couldn't get the version information of plug-ins.")); | ||
| 705 | } | ||
| 706 | } else { | ||
| 707 | debug_print("update_check_plugin_cb: modal dialog exists or incorporation is active. Disabling update dialog.\n"); | ||
| 708 | } | ||
| 709 | |||
| 710 | g_string_free(text, TRUE); | ||
| 711 | g_slist_foreach(list, (GFunc)download_plugin_info_free, NULL); | ||
| 712 | g_slist_free(list); | ||
| 713 | |||
| 714 | gdk_threads_leave(); | ||
| 715 | } | ||
| 716 | |||
| 717 | void update_check_plugin(gboolean show_dialog_always) | ||
| 718 | { | ||
| 719 | gchar buf[1024]; | ||
| 720 | |||
| 721 | if (!check_plugin_url) { | ||
| 722 | #ifdef G_OS_WIN32 | ||
| 723 | g_snprintf(buf, sizeof(buf), "%s?ver=%s&os=win", PLUGIN_VERSION_URI, VERSION); | ||
| 724 | #else | ||
| 725 | if (strstr(TARGET_ALIAS, "linux")) | ||
| 726 | g_snprintf(buf, sizeof(buf), "%s?ver=%s&os=linux", PLUGIN_VERSION_URI, VERSION); | ||
| 727 | else | ||
| 728 | g_snprintf(buf, sizeof(buf), "%s?ver=%s&os=other", PLUGIN_VERSION_URI, VERSION); | ||
| 729 | #endif | ||
| 730 | update_check_set_check_plugin_url(buf); | ||
| 731 | } | ||
| 732 | spawn_curl(check_plugin_url, update_check_plugin_cb, GINT_TO_POINTER(show_dialog_always)); | ||
| 733 | } | ||
| 734 | #endif /* USE_UPDATE_CHECK_PLUGIN */ | ||
| 735 | |||
| 736 | void update_check(gboolean show_dialog_always) | ||
| 737 | { | ||
| 738 | gchar buf[1024]; | ||
| 739 | |||
| 740 | if (!check_url) { | ||
| 741 | #ifdef G_OS_WIN32 | ||
| 742 | g_snprintf(buf, sizeof(buf), "%s?ver=%s&os=win", VERSION_URI, VERSION); | ||
| 743 | #else | ||
| 744 | if (strstr(TARGET_ALIAS, "linux")) | ||
| 745 | g_snprintf(buf, sizeof(buf), "%s?ver=%s&os=linux", VERSION_URI, VERSION); | ||
| 746 | else | ||
| 747 | g_snprintf(buf, sizeof(buf), "%s?ver=%s&os=other", VERSION_URI, VERSION); | ||
| 748 | #endif | ||
| 749 | update_check_set_check_url(buf); | ||
| 750 | } | ||
| 751 | spawn_curl(check_url, update_check_cb, GINT_TO_POINTER(show_dialog_always)); | ||
| 752 | } | ||
| 753 | |||
| 754 | void update_check_set_check_url(const gchar *url) | ||
| 755 | { | ||
| 756 | if (check_url) | ||
| 757 | g_free(check_url); | ||
| 758 | check_url = g_strdup(url); | ||
| 759 | |||
| 760 | if (url) | ||
| 761 | debug_print("update_check_set_check_url: check URL was set to: %s\n", url); | ||
| 762 | else | ||
| 763 | debug_print("update_check_set_check_url: check URL was unset.\n"); | ||
| 764 | } | ||
| 765 | |||
| 766 | const gchar *update_check_get_check_url(void) | ||
| 767 | { | ||
| 768 | return check_url; | ||
| 769 | } | ||
| 770 | |||
| 771 | void update_check_set_download_url(const gchar *url) | ||
| 772 | { | ||
| 773 | if (download_url) | ||
| 774 | g_free(download_url); | ||
| 775 | download_url = g_strdup(url); | ||
| 776 | |||
| 777 | if (url) | ||
| 778 | debug_print("update_check_set_download_url: download URL was set to: %s\n", url); | ||
| 779 | else | ||
| 780 | debug_print("update_check_set_download_url: download URL was unset.\n"); | ||
| 781 | } | ||
| 782 | |||
| 783 | const gchar *update_check_get_download_url(void) | ||
| 784 | { | ||
| 785 | return download_url; | ||
| 786 | } | ||
| 787 | |||
| 788 | void update_check_set_jump_url(const gchar *url) | ||
| 789 | { | ||
| 790 | if (jump_url) | ||
| 791 | g_free(jump_url); | ||
| 792 | jump_url = g_strdup(url); | ||
| 793 | } | ||
| 794 | |||
| 795 | const gchar *update_check_get_jump_url(void) | ||
| 796 | { | ||
| 797 | return jump_url; | ||
| 798 | } | ||
| 799 | |||
| 800 | #ifdef USE_UPDATE_CHECK_PLUGIN | ||
| 801 | void update_check_set_check_plugin_url(const gchar *url) | ||
| 802 | { | ||
| 803 | if (check_plugin_url) | ||
| 804 | g_free(check_plugin_url); | ||
| 805 | check_plugin_url = g_strdup(url); | ||
| 806 | } | ||
| 807 | |||
| 808 | const gchar *update_check_get_check_plugin_url(void) | ||
| 809 | { | ||
| 810 | return check_plugin_url; | ||
| 811 | } | ||
| 812 | |||
| 813 | void update_check_set_jump_plugin_url(const gchar *url) | ||
| 814 | { | ||
| 815 | if (jump_plugin_url) | ||
| 816 | g_free(jump_plugin_url); | ||
| 817 | jump_plugin_url = g_strdup(url); | ||
| 818 | } | ||
| 819 | |||
| 820 | const gchar *update_check_get_jump_plugin_url(void) | ||
| 821 | { | ||
| 822 | return jump_plugin_url; | ||
| 823 | } | ||
| 824 | #endif /* USE_UPDATE_CHECK_PLUGIN */ | ||
| 825 | |||
| 826 | #endif /* USE_UPDATE_CHECK */ | ||
