~ubuntu-branches/ubuntu/raring/nautilus-sendto-universe/raring

« back to all changes in this revision

Viewing changes to pidgin_plugin/nautilus-sendto-plugin.c

  • Committer: Bazaar Package Importer
  • Author(s): Onkar Shinde
  • Date: 2009-07-30 23:31:20 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090730233120-ut3baoo6lde6q9gk
Tags: 1.1.6-0ubuntu1
* New upstream release. (LP: #377192)
* debian/control
  - Remove libglade2-dev build dependency.
  - Bump version of libgtk2.0-dev build dependency to 2.12.
  - Remove empathy related build dependencies.
  - Update description to remove empathy references.
* debian/nautilus-sendto-universe.install
  - Remove empathy plugin.
* debian/patches/10_fix_empathy_2.26.patch
  - Remove. Not needed anymore.
* debian/README.Debian.
  - Update to remove empathy references.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
#endif
32
32
 
33
33
#include <glib.h>
 
34
#include <glib/gstdio.h>
34
35
#include <glib/gi18n.h>
35
 
#include <sys/stat.h>
36
 
#include <sys/types.h>
37
 
#include <fcntl.h>
38
 
#include <unistd.h>
39
 
#include <dirent.h>
 
36
#include <errno.h>
40
37
 
41
38
#include "conversation.h"
42
39
#include "core.h"
59
56
 
60
57
static guint take_spool_files_handler;
61
58
static gboolean taking_files;
62
 
static GString *buddies_str;
 
59
static GString *buddies_str = NULL;
 
60
 
 
61
static char *
 
62
get_buddies_path (void)
 
63
{
 
64
        return g_build_filename (g_get_home_dir(), PLUGIN_HOME, B_ONLINE, NULL);
 
65
}
63
66
 
64
67
static void
65
68
get_online_buddies (PurpleBlistNode *node, GString *str){
66
69
 
67
 
    PurpleBlistNode *aux;
68
 
 
69
 
    if (node == NULL)
70
 
        return;
71
 
    for (aux = node ; aux != NULL ;aux = aux->next){
72
 
        if (PURPLE_BLIST_NODE_IS_BUDDY(aux)){
73
 
            PurpleBuddy *buddy;
74
 
            buddy = (PurpleBuddy*)aux;
75
 
            if (PURPLE_BUDDY_IS_ONLINE(buddy)){
76
 
                gchar *alias;
77
 
 
78
 
                if (buddy->alias == NULL){
79
 
                    if (buddy->server_alias == NULL){
80
 
                        alias = g_strdup (buddy->name);
81
 
                    }else{
82
 
                        alias = g_strdup (buddy->server_alias);
83
 
                    }
84
 
                }else{
85
 
                    alias = g_strdup (buddy->alias);
86
 
                }
87
 
 
88
 
                g_string_append_printf (str,"%s\n%s\n%s\n%s\n",                                 
89
 
                                        buddy->account->username,
90
 
                                        buddy->name,
91
 
                                        alias,
92
 
                                        buddy->account->protocol_id);
93
 
                
94
 
                g_free (alias);
95
 
            }
96
 
        }else{
97
 
            get_online_buddies (aux->child, str);
98
 
        }
99
 
    }
100
 
 
101
 
}
102
 
 
103
 
static void
104
 
save_online_buddies (PurpleBuddy *buddy, gpointer data){
105
 
    PurpleBuddyList *blist;
106
 
    GString *str;
107
 
    gchar *fd_name;
108
 
    FILE *fd;
109
 
 
110
 
    fd_name = g_build_path ("/", g_get_home_dir(), PLUGIN_HOME, 
111
 
                            B_ONLINE, NULL);
112
 
 
113
 
    blist = purple_get_blist();
114
 
    str = g_string_new ("---\n");
115
 
    get_online_buddies (blist->root, str);       
116
 
    str = g_string_append (str, "---\n");
117
 
 
118
 
    if (!g_string_equal (buddies_str, str)){
119
 
            GError *err = NULL;
120
 
            if (g_file_set_contents (fd_name, str->str, str->len, &err) == FALSE) {
121
 
                    purple_debug_info ("nautilus", "couldn't save '%s': %s\n", fd_name, err->message);
122
 
                    g_error_free (err);
123
 
                    g_string_free (buddies_str, TRUE);
124
 
            } else {
125
 
                    purple_debug_info ("nautilus", "saved blist online\n");
126
 
                    g_string_free (buddies_str, TRUE);
127
 
                    buddies_str = str;
128
 
            }
129
 
    }else{
130
 
        g_string_free (str, TRUE);
131
 
        purple_debug_info ("nautilus", "don't save blist online. No change\n");
132
 
    }
133
 
    g_free (fd_name);
134
 
 
135
 
}
136
 
 
137
 
static void
138
 
init_plugin_stuff (){
139
 
    gchar *plugin_home;
140
 
    gchar *spool;
141
 
    gchar *spool_tmp;
142
 
    plugin_home = g_build_path ("/", g_get_home_dir(), 
143
 
                                PLUGIN_HOME, NULL);
144
 
    
145
 
    if (!g_file_test (plugin_home,G_FILE_TEST_IS_DIR)){
146
 
        mkdir (plugin_home, 0755);
147
 
        purple_debug_info ("nautilus", "creating: %s\n",plugin_home);
148
 
    }
149
 
    g_free (plugin_home);
150
 
    spool = g_build_path ("/", g_get_home_dir(), PLUGIN_HOME, "spool", NULL);
151
 
    if (!g_file_test (spool,G_FILE_TEST_IS_DIR)){
152
 
        mkdir (spool, 0755);
153
 
        purple_debug_info ("nautilus", "creating: %s\n", spool);
154
 
    }
155
 
    g_free (spool);
156
 
    spool_tmp = g_build_path ("/", g_get_home_dir(), PLUGIN_HOME, "spool", "tmp", NULL);
157
 
    if (!g_file_test (spool_tmp,G_FILE_TEST_IS_DIR)){
158
 
        mkdir (spool_tmp, 0755);
159
 
        purple_debug_info ("nautilus", "creating: %s\n", spool_tmp);
160
 
    }
161
 
    g_free (spool_tmp);
 
70
        PurpleBlistNode *aux;
 
71
 
 
72
        if (node == NULL)
 
73
                return;
 
74
 
 
75
        for (aux = node ; aux != NULL ;aux = aux->next) {
 
76
                if (PURPLE_BLIST_NODE_IS_BUDDY(aux)){
 
77
                        PurpleBuddy *buddy;
 
78
                        buddy = (PurpleBuddy*)aux;
 
79
                        if (PURPLE_BUDDY_IS_ONLINE(buddy)){
 
80
                                char *alias;
 
81
 
 
82
                                if (buddy->alias == NULL){
 
83
                                        if (buddy->server_alias == NULL){
 
84
                                                alias = g_strdup (buddy->name);
 
85
                                        }else{
 
86
                                                alias = g_strdup (buddy->server_alias);
 
87
                                        }
 
88
                                }else{
 
89
                                        alias = g_strdup (buddy->alias);
 
90
                                }
 
91
 
 
92
                                g_string_append_printf (str,"%s\n%s\n%s\n%s\n",
 
93
                                                        buddy->account->username,
 
94
                                                        buddy->name,
 
95
                                                        alias,
 
96
                                                        buddy->account->protocol_id);
 
97
 
 
98
                                g_free (alias);
 
99
                        }
 
100
                } else {
 
101
                        get_online_buddies (aux->child, str);
 
102
                }
 
103
        }
 
104
}
 
105
 
 
106
static void
 
107
save_online_buddies (PurpleBuddy *buddy, gpointer data)
 
108
{
 
109
        PurpleBuddyList *blist;
 
110
        GString *str;
 
111
        char *fd_name;
 
112
 
 
113
        fd_name = get_buddies_path ();
 
114
 
 
115
        blist = purple_get_blist();
 
116
        str = g_string_new ("---\n");
 
117
        get_online_buddies (blist->root, str);
 
118
        str = g_string_append (str, "---\n");
 
119
 
 
120
        if (!g_string_equal (buddies_str, str)) {
 
121
                GError *err = NULL;
 
122
                if (g_file_set_contents (fd_name, str->str, str->len, &err) == FALSE) {
 
123
                        purple_debug_info ("nautilus", "couldn't save '%s': %s\n", fd_name, err->message);
 
124
                        g_error_free (err);
 
125
                        g_string_free (str, TRUE);
 
126
                } else {
 
127
                        purple_debug_info ("nautilus", "saved blist online\n");
 
128
                        g_string_free (buddies_str, TRUE);
 
129
                        buddies_str = str;
 
130
                }
 
131
        } else {
 
132
                g_string_free (str, TRUE);
 
133
                purple_debug_info ("nautilus", "don't save blist online. No change\n");
 
134
        }
 
135
        g_free (fd_name);
 
136
}
 
137
 
 
138
static gboolean
 
139
init_plugin_stuff (void)
 
140
{
 
141
        char *spool_tmp;
 
142
        gboolean retval;
 
143
 
 
144
        spool_tmp = g_build_filename (g_get_home_dir(), PLUGIN_HOME, "spool", "tmp", NULL);
 
145
        if (g_mkdir_with_parents (spool_tmp, 0755) < 0) {
 
146
                int error = errno;
 
147
                g_warning ("Failed to create '%s': %s", spool_tmp, g_strerror (error));
 
148
                retval = FALSE;
 
149
        } else {
 
150
                retval = TRUE;
 
151
        }
 
152
 
 
153
        g_free (spool_tmp);
 
154
 
 
155
        return retval;
162
156
}
163
157
 
164
158
static void
165
159
send_file (GString *username, GString *cname,
166
 
           GString *protocol, GString *file){
167
 
 
168
 
    PurpleAccount *account;
169
 
    PurpleXfer *xfer;
170
 
 
171
 
    account = purple_accounts_find (username->str, protocol->str);
172
 
    if (account == NULL)
173
 
        return;
174
 
 
175
 
    serv_send_file (account->gc, cname->str, file->str);
 
160
           GString *protocol, GString *file)
 
161
{
 
162
        PurpleAccount *account;
 
163
 
 
164
        account = purple_accounts_find (username->str, protocol->str);
 
165
        if (account == NULL)
 
166
                return;
 
167
 
 
168
        serv_send_file (account->gc, cname->str, file->str);
176
169
}
177
170
 
178
171
static void
179
 
process_file (gchar *file){
180
 
    GIOChannel *io;
181
 
    GString *username;
182
 
    GString *cname;
183
 
    GString *protocol;
184
 
    GString *file_to_send;
185
 
    
186
 
    username = g_string_new ("");
187
 
    cname = g_string_new ("");
188
 
    protocol = g_string_new ("");
189
 
    file_to_send = g_string_new ("");
190
 
    
191
 
    io = g_io_channel_new_file (file,"r",NULL);
192
 
    if (io == NULL)
193
 
        return;
194
 
 
195
 
    purple_debug_info ("nautilus","Open spool file : %s\n",file);
196
 
    g_io_channel_read_line_string (io, username, NULL, NULL);
197
 
    username = g_string_truncate (username, username->len - 1);
198
 
    g_io_channel_read_line_string (io, cname, NULL, NULL);
199
 
    cname = g_string_truncate (cname, cname->len - 1);
200
 
    g_io_channel_read_line_string (io, protocol, NULL, NULL);
201
 
    protocol = g_string_truncate (protocol, protocol->len - 1);
202
 
 
203
 
    while (G_IO_STATUS_EOF != 
204
 
           g_io_channel_read_line_string (io,file_to_send, 
205
 
                                          NULL, NULL))
206
 
    {   
207
 
        if (file_to_send->len <=1)
208
 
            continue;
209
 
        file_to_send = g_string_truncate (file_to_send, 
210
 
                                          file_to_send->len - 1);
211
 
        send_file (username, cname, 
212
 
                   protocol, file_to_send);
213
 
    }
214
 
 
215
 
    g_string_free (username, TRUE);
216
 
    g_string_free (cname, TRUE);
217
 
    g_string_free (protocol, TRUE);
218
 
    g_string_free (file_to_send, TRUE);
219
 
    g_io_channel_shutdown (io, TRUE, NULL);
220
 
    remove (file);    
 
172
process_file (const char *file)
 
173
{
 
174
        GIOChannel *io;
 
175
        GString *username;
 
176
        GString *cname;
 
177
        GString *protocol;
 
178
        GString *file_to_send;
 
179
 
 
180
        username = g_string_new ("");
 
181
        cname = g_string_new ("");
 
182
        protocol = g_string_new ("");
 
183
        file_to_send = g_string_new ("");
 
184
 
 
185
        io = g_io_channel_new_file (file,"r",NULL);
 
186
        if (io == NULL)
 
187
                return;
 
188
 
 
189
        purple_debug_info ("nautilus","Open spool file : %s\n",file);
 
190
        g_io_channel_read_line_string (io, username, NULL, NULL);
 
191
        username = g_string_truncate (username, username->len - 1);
 
192
        g_io_channel_read_line_string (io, cname, NULL, NULL);
 
193
        cname = g_string_truncate (cname, cname->len - 1);
 
194
        g_io_channel_read_line_string (io, protocol, NULL, NULL);
 
195
        protocol = g_string_truncate (protocol, protocol->len - 1);
 
196
 
 
197
        while (G_IO_STATUS_EOF != g_io_channel_read_line_string (io,file_to_send, NULL, NULL)) {
 
198
                if (file_to_send->len <=1)
 
199
                        continue;
 
200
                file_to_send = g_string_truncate (file_to_send,
 
201
                                                  file_to_send->len - 1);
 
202
                send_file (username, cname,
 
203
                           protocol, file_to_send);
 
204
        }
 
205
 
 
206
        g_string_free (username, TRUE);
 
207
        g_string_free (cname, TRUE);
 
208
        g_string_free (protocol, TRUE);
 
209
        g_string_free (file_to_send, TRUE);
 
210
        g_io_channel_shutdown (io, TRUE, NULL);
 
211
        remove (file);
221
212
}
222
213
 
223
 
gint
224
 
take_spool_files(){
225
 
    DIR *dir;
226
 
    struct dirent *ep;
227
 
    gchar *plugin_spool;
228
 
    
229
 
    if (taking_files == FALSE){
230
 
            taking_files = TRUE;
231
 
            plugin_spool = g_build_path ("/", g_get_home_dir(),PLUGIN_HOME,"spool", NULL);
232
 
            dir = opendir (plugin_spool);
233
 
            g_free (plugin_spool);
234
 
            if (dir == NULL){
235
 
                    purple_debug_info ("nautilus","Can't open the spool dir\n");
236
 
            }else{
237
 
                    while (ep = readdir(dir)){
238
 
                            gchar *file;
239
 
 
240
 
                            if ((strcmp (ep->d_name,".")==0)   || 
241
 
                                (strcmp (ep->d_name, "..")==0) ||
242
 
                                (strcmp (ep->d_name, "tmp")==0))
243
 
                                    continue ;
244
 
                                                                            
245
 
                            file = g_build_path ("/", g_get_home_dir(), 
246
 
                                                 PLUGIN_HOME,"spool", 
247
 
                                                 ep->d_name, NULL);
248
 
                            process_file (file);
249
 
                            g_free (file);
250
 
                    }
251
 
                    closedir(dir);
252
 
            }
253
 
            taking_files = FALSE;
254
 
    }
255
 
    return TRUE;
 
214
static int
 
215
take_spool_files (gpointer user_data)
 
216
{
 
217
        char *plugin_spool;
 
218
 
 
219
        if (taking_files == FALSE)
 
220
        {
 
221
                GDir *dir;
 
222
                GError *err = NULL;
 
223
 
 
224
                taking_files = TRUE;
 
225
                plugin_spool = g_build_filename (g_get_home_dir(), PLUGIN_HOME, "spool", NULL);
 
226
                dir = g_dir_open (plugin_spool, 0, &err);
 
227
                g_free (plugin_spool);
 
228
                if (dir == NULL) {
 
229
                        purple_debug_info ("nautilus","Can't open the spool dir: %s\n", err->message);
 
230
                        g_error_free (err);
 
231
                } else {
 
232
                        const char *filename;
 
233
 
 
234
                        filename = g_dir_read_name (dir);
 
235
                        while (filename) {
 
236
                                char *file;
 
237
 
 
238
                                if (g_str_equal (filename, "tmp")) {
 
239
                                        filename = g_dir_read_name (dir);
 
240
                                        continue;
 
241
                                }
 
242
 
 
243
                                file = g_build_filename (g_get_home_dir(),
 
244
                                                         PLUGIN_HOME, "spool",
 
245
                                                         filename, NULL);
 
246
                                process_file (file);
 
247
                                g_free (file);
 
248
 
 
249
                                filename = g_dir_read_name (dir);
 
250
                        }
 
251
                        g_dir_close (dir);
 
252
                }
 
253
                taking_files = FALSE;
 
254
        }
 
255
        return TRUE;
256
256
}
257
257
 
258
258
static gboolean
259
 
plugin_load(PurplePlugin *plugin)
 
259
plugin_load (PurplePlugin *plugin)
260
260
{
261
261
        void *blist_handle;
262
 
        
263
 
        init_plugin_stuff ();
 
262
 
 
263
        if (init_plugin_stuff () == FALSE)
 
264
                return FALSE;
264
265
        buddies_str = g_string_new ("");
265
266
 
266
267
        blist_handle = purple_blist_get_handle();
272
273
                               plugin, (PurpleCallback) save_online_buddies,
273
274
                               NULL);
274
275
        taking_files = FALSE;
275
 
        take_spool_files_handler = purple_timeout_add (3000,
276
 
                                                     take_spool_files,
277
 
                                                     NULL);
 
276
        take_spool_files_handler = purple_timeout_add_seconds (3, (GSourceFunc) take_spool_files, NULL);
278
277
 
279
278
        /* And save a list already */
280
279
        save_online_buddies (NULL, NULL);
283
282
}
284
283
 
285
284
static gboolean
286
 
plugin_unload() {
287
 
    gchar *fd_name;
288
 
 
289
 
    fd_name = g_build_path ("/", g_get_home_dir(), PLUGIN_HOME, 
290
 
                            B_ONLINE, NULL);    
291
 
    purple_timeout_remove (take_spool_files_handler);
292
 
    g_unlink (fd_name);
293
 
    g_free(fd_name);
294
 
    g_string_free (buddies_str, TRUE);
295
 
    purple_debug_info ("nautilus", "Stop nautilus plugin\n");
296
 
    
297
 
    return TRUE;
 
285
plugin_unload (PurplePlugin *plugin)
 
286
{
 
287
        void *blist_handle;
 
288
        char *fd_name;
 
289
 
 
290
        blist_handle = purple_blist_get_handle();
 
291
 
 
292
        purple_signal_disconnect (blist_handle, "buddy-signed-on",
 
293
                                  plugin, (PurpleCallback) save_online_buddies);
 
294
        purple_signal_disconnect (blist_handle, "buddy-signed-off",
 
295
                                  plugin, (PurpleCallback) save_online_buddies);
 
296
 
 
297
        fd_name = get_buddies_path ();
 
298
        purple_timeout_remove (take_spool_files_handler);
 
299
        g_unlink (fd_name);
 
300
        g_free (fd_name);
 
301
        g_string_free (buddies_str, TRUE);
 
302
        buddies_str = NULL;
 
303
        purple_debug_info ("nautilus", "Stop nautilus plugin\n");
 
304
 
 
305
        return TRUE;
298
306
}
299
307
 
300
308
static gboolean
301
309
force_load_once (gpointer data)
302
310
{
303
 
    PurplePlugin *plugin = (PurplePlugin *)data;
304
 
    if (!purple_prefs_get_bool ("/plugins/gtk/nautilus/auto_loaded")) {
305
 
            purple_debug_info ("nautilus", "Force loading nautilus plugin\n");
306
 
            purple_plugin_load (plugin);
 
311
        PurplePlugin *plugin = (PurplePlugin *)data;
 
312
 
 
313
        if (!purple_prefs_get_bool ("/plugins/gtk/nautilus/auto_loaded")) {
 
314
                purple_debug_info ("nautilus", "Force loading nautilus plugin\n");
 
315
                purple_plugin_load (plugin);
307
316
                purple_plugins_save_loaded (PIDGIN_PREFS_ROOT "/plugins/loaded");
308
 
            purple_prefs_set_bool ("/plugins/gtk/nautilus/auto_loaded", TRUE);
309
 
    }
 
317
                purple_prefs_set_bool ("/plugins/gtk/nautilus/auto_loaded", TRUE);
 
318
        }
310
319
 
311
 
    return FALSE;
 
320
        return FALSE;
312
321
}
313
322
 
314
323
static void 
315
 
init_plugin(PurplePlugin *plugin) {
316
 
    purple_prefs_add_none ("/plugins/gtk/nautilus");
317
 
    purple_prefs_add_bool ("/plugins/gtk/nautilus/auto_loaded", FALSE);
 
324
init_plugin(PurplePlugin *plugin)
 
325
{
 
326
        purple_prefs_add_none ("/plugins/gtk/nautilus");
 
327
        purple_prefs_add_bool ("/plugins/gtk/nautilus/auto_loaded", FALSE);
318
328
        g_idle_add(force_load_once, plugin);
319
329
}
320
330
 
321
331
static PurplePluginInfo info = {
322
 
    PURPLE_PLUGIN_MAGIC,                /* api version */
323
 
    PURPLE_MAJOR_VERSION,
324
 
    PURPLE_MINOR_VERSION,
325
 
    PURPLE_PLUGIN_STANDARD,             /* type */
326
 
    PIDGIN_PLUGIN_TYPE,                 /* ui requirement */
327
 
    PURPLE_PRIORITY_DEFAULT,            /* flags */
328
 
    NULL,                               /* dependencies */
329
 
    PURPLE_PRIORITY_DEFAULT,            /* priority */
330
 
 
331
 
    "gtk-nautilus",                     /* id */
332
 
    N_("Nautilus Integration"),         /* name */
333
 
    "0.8",                              /* version */
334
 
    N_("Provides integration with Nautilus"),           /* summary */ 
335
 
    N_("Provides integration with Nautilus"),           /* description */
336
 
 
337
 
    "Roberto Majadas <roberto.majadas@openshine.com>",  /* author */
338
 
    "www.gnome.org",                    /* homepage */
339
 
 
340
 
    plugin_load,                        /* load */
341
 
    plugin_unload,                      /* unload */
342
 
    NULL,                               /* destroy */
343
 
    NULL,                               /* ui info */
344
 
    NULL,                               /* extra info */
345
 
    NULL                                /* actions info */
 
332
        PURPLE_PLUGIN_MAGIC,            /* api version */
 
333
        PURPLE_MAJOR_VERSION,
 
334
        PURPLE_MINOR_VERSION,
 
335
        PURPLE_PLUGIN_STANDARD,         /* type */
 
336
        PIDGIN_PLUGIN_TYPE,                     /* ui requirement */
 
337
        PURPLE_PRIORITY_DEFAULT,                /* flags */
 
338
        NULL,                           /* dependencies */
 
339
        PURPLE_PRIORITY_DEFAULT,                /* priority */
 
340
 
 
341
        "gtk-nautilus",                 /* id */
 
342
        N_("Nautilus Integration"),             /* name */
 
343
        "0.8",                          /* version */
 
344
        N_("Provides integration with Nautilus"),               /* summary */ 
 
345
        N_("Provides integration with Nautilus"),               /* description */
 
346
 
 
347
        "Roberto Majadas <roberto.majadas@openshine.com>",      /* author */
 
348
        "www.gnome.org",                    /* homepage */
 
349
 
 
350
        plugin_load,                    /* load */
 
351
        plugin_unload,                  /* unload */
 
352
        NULL,                           /* destroy */
 
353
        NULL,                           /* ui info */
 
354
        NULL,                           /* extra info */
 
355
        NULL                            /* actions info */
346
356
};
347
357
 
348
358
PURPLE_INIT_PLUGIN(nautilus, init_plugin, info)