Statistics
| Revision:

root / src / update_check.c @ 2164

History | View | Annotate | Download (6 KB)

1
/*
2
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3
 * Copyright (C) 1999-2009 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
#include "defs.h"
25

    
26
#include <glib.h>
27
#include <glib/gi18n.h>
28
#include <gdk/gdkkeysyms.h>
29
#include <gtk/gtk.h>
30

    
31
#include <stdlib.h>
32
#include <unistd.h>
33

    
34
#include "update_check.h"
35
#include "manage_window.h"
36
#include "gtkutils.h"
37
#include "alertpanel.h"
38
#include "prefs_common.h"
39
#include "socket.h"
40
#include "utils.h"
41
#include "version.h"
42

    
43

    
44
static gboolean compare_version(gint major, gint minor, gint micro,
45
                                const gchar *extra)
46
{
47
        debug_print("comparing %d.%d.%d.%s <> " VERSION "\n", major, minor, micro, extra ? extra : "");
48

    
49
        if (major > MAJOR_VERSION)
50
                return TRUE;
51
        if (major < MAJOR_VERSION)
52
                return FALSE;
53
        if (minor > MINOR_VERSION)
54
                return TRUE;
55
        if (minor < MINOR_VERSION)
56
                return FALSE;
57
        if (micro > MICRO_VERSION)
58
                return TRUE;
59
        if (micro < MICRO_VERSION)
60
                return FALSE;
61
        if (extra) {
62
                if (strcmp(extra, EXTRA_VERSION) > 0)
63
                        return TRUE;
64
        }
65

    
66
        return FALSE;
67
}
68

    
69
static void parse_version_string(const gchar *ver, gint *major, gint *minor,
70
                                 gint *micro, gchar **extra)
71
{
72
        gchar **vers;
73

    
74
        vers = g_strsplit(ver, ".", -1);
75
        if (vers[0]) {
76
                *major = atoi(vers[0]);
77
                if (vers[1]) {
78
                        *minor = atoi(vers[1]);
79
                        if (vers[2]) {
80
                                *micro = atoi(vers[2]);
81
                                if (vers[3]) {
82
                                        *extra = g_strdup(vers[3]);
83
                                }
84
                        }
85
                }
86
        }
87
        g_strfreev(vers);
88
}
89

    
90
static void update_dialog(const gchar *new_ver, gboolean manual)
91
{
92
        gchar buf[1024];
93
        AlertValue val;
94

    
95
        if (new_ver)
96
                g_snprintf(buf, sizeof(buf), "%s\n\n%s -> %s",
97
                           _("The newer version of Sylpheed found.\n"
98
                             "Upgrade now?"),
99
                           VERSION, new_ver);
100
        else
101
                g_snprintf(buf, sizeof(buf), "%s",
102
                           _("The newer version of Sylpheed found.\n"
103
                             "Upgrade now?"));
104

    
105
        val = alertpanel_full(_("New version found"), buf,
106
                              ALERT_QUESTION,
107
                              G_ALERTDEFAULT,
108
                              manual ? FALSE : TRUE,
109
                              GTK_STOCK_YES, GTK_STOCK_NO, NULL);
110
        if ((val & G_ALERT_VALUE_MASK) == G_ALERTDEFAULT) {
111
                open_uri(HOMEPAGE_URI, prefs_common.uri_cmd);
112
        }
113
        if (val & G_ALERTDISABLE) {
114
                prefs_common.auto_update_check = FALSE;
115
        }
116
}
117

    
118
static gint child_stdout;
119

    
120
static void update_check_cb(GPid pid, gint status, gpointer data)
121
{
122
        gchar **lines;
123
        gchar *key, *val, *p;
124
        gchar *new_ver = NULL;
125
        gint i;
126
#ifdef DEVEL_VERSION
127
        gboolean cur_ver_is_release = FALSE;
128
#else
129
        gboolean cur_ver_is_release = TRUE;
130
#endif
131
        gboolean result = FALSE;
132
        gboolean got_version = FALSE;
133
        gboolean show_dialog_always = (gboolean)data;
134
        gchar buf[BUFFSIZE];
135
        ssize_t size;
136

    
137
        debug_print("update_check_cb\n");
138

    
139
        if (!child_stdout) {
140
                g_spawn_close_pid(pid);
141
                return;
142
        }
143

    
144
        size = read(child_stdout, buf, sizeof(buf) - 1);
145
        if (size < 0) {
146
                fd_close(child_stdout);
147
                child_stdout = 0;
148
                g_spawn_close_pid(pid);
149
                return;
150
        }
151
        buf[size] = '\0';
152

    
153
        fd_close(child_stdout);
154
        child_stdout = 0;
155
        g_spawn_close_pid(pid);
156

    
157
        lines = g_strsplit(buf, "\n", -1);
158

    
159
        for (i = 0; lines[i] != NULL; i++) {
160
                gint major = 0, minor = 0, micro = 0;
161
                gchar *extra = NULL;
162

    
163
                debug_print("update_check: %s\n", lines[i]);
164
                p = strchr(lines[i], '=');
165
                if (!p) continue;
166
                key = g_strndup(lines[i], p - lines[i]);
167
                val = p + 1;
168

    
169
                if (!strcmp(key, "RELEASE")) {
170
                        parse_version_string(val, &major, &minor, &micro,
171
                                             &extra);
172
                        result = compare_version(major, minor, micro, extra);
173
                } else if (!cur_ver_is_release && !strcmp(key, "DEVEL")) {
174
                        parse_version_string(val, &major, &minor, &micro,
175
                                             &extra);
176
                        result = compare_version(major, minor, micro, extra);
177
                }
178

    
179
                if (major + minor + micro != 0)
180
                        got_version = TRUE;
181

    
182
                if (result) {
183
                        new_ver = g_strdup_printf("%d.%d.%d%s", major, minor, micro, extra ? extra : "");
184
                        debug_print("update_check: new ver: %s\n", new_ver);
185
                        g_free(extra);
186
                        g_free(key);
187
                        break;
188
                }
189
                g_free(extra);
190
                g_free(key);
191
        }
192

    
193
        g_strfreev(lines);
194

    
195
        if (result)
196
                update_dialog(new_ver, show_dialog_always);
197
        else if (show_dialog_always) {
198
                if (got_version)
199
                        alertpanel_message(_("Information"),
200
                                           _("Sylpheed is already the latest version."),
201
                                           ALERT_NOTICE);
202
                else
203
                        alertpanel_error(_("Couldn't get the version information."));
204
        }
205
        g_free(new_ver);
206
}
207

    
208
void update_check(gboolean show_dialog_always)
209
{
210
        gchar *cmdline[6] = {"curl", "--silent"};
211
        GPid pid;
212
        GError *error = NULL;
213

    
214
        if (child_stdout > 0) {
215
                debug_print("update check is in progress\n");
216
                return;
217
        }
218

    
219
        child_stdout = 0;
220

    
221
        debug_print("update_check: getting latest version from http://sylpheed.sraoss.jp/version.txt\n");
222

    
223
        cmdline[2] = "http://sylpheed.sraoss.jp/version.txt?";
224
        if (prefs_common.use_http_proxy && prefs_common.http_proxy_host &&
225
            prefs_common.http_proxy_host[0] != '\0') {
226
                cmdline[3] = "--proxy";
227
                cmdline[4] = prefs_common.http_proxy_host;
228
                cmdline[5] = NULL;
229
        } else
230
                cmdline[3] = NULL;
231

    
232
        if (g_spawn_async_with_pipes
233
                (NULL, cmdline, NULL,
234
                 G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH,
235
                 NULL, NULL, &pid,
236
                 NULL, &child_stdout, NULL, &error) == FALSE) {
237
                g_warning("Couldn't execute curl");
238
                if (error) {
239
                        g_warning("g_spawn_async_with_pipes: %s",
240
                                  error->message);
241
                        g_error_free(error);
242
                }
243
                return;
244
        }
245
        if (pid == 0) {
246
                g_warning("Couldn't get PID of child process");
247
                if (child_stdout) {
248
                        fd_close(child_stdout);
249
                        child_stdout = 0;
250
                }
251
                return;
252
        }
253

    
254
        g_child_watch_add(pid, update_check_cb, (gpointer)show_dialog_always);
255
}