~ubuntu-branches/ubuntu/saucy/cairo-dock-plug-ins/saucy

« back to all changes in this revision

Viewing changes to xfce-integration/src/applet-thunar-vfs.c

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2009-08-26 21:07:39 UTC
  • Revision ID: james.westby@ubuntu.com-20090826210739-gyjuuqezrzuluao4
Tags: upstream-2.0.8.1
ImportĀ upstreamĀ versionĀ 2.0.8.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
* This file is a part of the Cairo-Dock project
 
3
*
 
4
* Copyright : (C) see the 'copyright' file.
 
5
* E-mail    : see the 'copyright' file.
 
6
*
 
7
* This program is free software; you can redistribute it and/or
 
8
* modify it under the terms of the GNU General Public License
 
9
* as published by the Free Software Foundation; either version 3
 
10
* of the License, or (at your option) any later version.
 
11
*
 
12
* This program is distributed in the hope that it will be useful,
 
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
* GNU General Public License for more details.
 
16
* You should have received a copy of the GNU General Public License
 
17
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
*/
 
19
 
 
20
/******************************************************************************
 
21
 
 
22
This file is a part of the cairo-dock program,
 
23
released under the terms of the GNU General Public License.
 
24
 
 
25
Written by Christophe Chapuis (for any bug report, please mail me to chris.chapuis@gmail.com)
 
26
Inspiration was taken from the "gnome-integration" plug-in...
 
27
 
 
28
******************************************************************************/
 
29
#include <string.h>
 
30
 
 
31
#include <thunar-vfs/thunar-vfs.h>
 
32
 
 
33
#include "applet-thunar-vfs.h"
 
34
 
 
35
static GHashTable *s_fm_MonitorHandleTable = NULL;
 
36
 
 
37
static void _vfs_backend_volume_added_callback (ThunarVfsVolumeManager *manager, gpointer volumes, gpointer *data);
 
38
static void _vfs_backend_volume_removed_callback (ThunarVfsVolumeManager *manager, gpointer volumes, gpointer *data);
 
39
///static void _vfs_backend_volume_modified_callback (ThunarVfsVolumeManager *manager, ThunarVfsVolume *pVolume, gpointer *data);
 
40
static ThunarVfsVolume *thunar_find_volume_from_path (ThunarVfsPath *pThunarPath);
 
41
 
 
42
static void _vfs_backend_free_monitor_data (gpointer *data)
 
43
{
 
44
        if (data != NULL)
 
45
        {
 
46
        ThunarVfsMonitor *pMonitor = thunar_vfs_monitor_get_default();
 
47
                ThunarVfsMonitorHandle *pHandle = data[2];
 
48
                thunar_vfs_monitor_remove(pMonitor, pHandle);  // le ThunarVFSMonitorHandle est-il libere lors du thunar_vfs_monitor_remove () ?
 
49
                g_free (data);
 
50
        }
 
51
}
 
52
 
 
53
gboolean init_vfs_backend (void)
 
54
{
 
55
        cd_message ("Initialisation du backend xfce-environnement");
 
56
 
 
57
        if (s_fm_MonitorHandleTable != NULL)
 
58
                g_hash_table_destroy (s_fm_MonitorHandleTable);
 
59
 
 
60
        s_fm_MonitorHandleTable = g_hash_table_new_full (g_str_hash,
 
61
                g_str_equal,
 
62
                g_free,
 
63
                (GDestroyNotify) _vfs_backend_free_monitor_data);
 
64
 
 
65
        thunar_vfs_init();
 
66
 
 
67
        return TRUE;
 
68
}
 
69
 
 
70
void stop_vfs_backend (void)
 
71
{
 
72
        cd_message ("Arret du backend xfce-environnement");
 
73
 
 
74
        if (s_fm_MonitorHandleTable != NULL)
 
75
        {
 
76
                g_hash_table_destroy (s_fm_MonitorHandleTable);
 
77
                s_fm_MonitorHandleTable = NULL;
 
78
        }
 
79
        
 
80
        ThunarVfsVolumeManager *pThunarVolumeManager = thunar_vfs_volume_manager_get_default();
 
81
        g_signal_handlers_disconnect_matched (pThunarVolumeManager, G_SIGNAL_MATCH_FUNC, 0, 0, NULL, _vfs_backend_volume_added_callback, NULL);
 
82
        g_signal_handlers_disconnect_matched (pThunarVolumeManager, G_SIGNAL_MATCH_FUNC, 0, 0, NULL, _vfs_backend_volume_removed_callback, NULL);
 
83
        ///g_signal_handlers_disconnect_matched (pThunarVolumeManager, G_SIGNAL_MATCH_FUNC, 0, 0, NULL, _vfs_backend_volume_modified_callback, NULL);
 
84
        
 
85
        thunar_vfs_shutdown();
 
86
}
 
87
 
 
88
 
 
89
 
 
90
static gboolean file_manager_get_file_info_from_desktop_link (const gchar *cBaseURI, gchar **cName, gchar **cURI, gchar **cIconName, gboolean *bIsDirectory, int *iVolumeID)
 
91
{
 
92
        cd_message ("%s (%s)", __func__, cBaseURI);
 
93
        GError *erreur = NULL;
 
94
 
 
95
        gchar *cFileData = NULL;
 
96
        int iFileSize = 0;
 
97
 
 
98
        ThunarVfsPath *pThunarPath = thunar_vfs_path_new(cBaseURI, &erreur);
 
99
        if (erreur != NULL)
 
100
        {
 
101
                cd_warning ("Attention : couldn't read %s (%s)", cBaseURI, erreur->message);
 
102
                g_error_free (erreur);
 
103
                return FALSE;
 
104
        }
 
105
        gchar *cFilePath = thunar_vfs_path_dup_string(pThunarPath);
 
106
        thunar_vfs_path_unref(pThunarPath);
 
107
        if (cFilePath == NULL)
 
108
        {
 
109
                cd_warning ("Attention : Couldn't retrieve path of %s", cBaseURI);
 
110
                return FALSE;
 
111
        }
 
112
 
 
113
        GKeyFile *pKeyFile = g_key_file_new ();
 
114
        g_key_file_load_from_file (pKeyFile,
 
115
                cFilePath,
 
116
                G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS,
 
117
                &erreur);
 
118
        g_free (cFilePath);
 
119
        if (erreur != NULL)
 
120
        {
 
121
                cd_warning ("Attention : %s", erreur->message);
 
122
                g_error_free (erreur);
 
123
                return FALSE;
 
124
        }
 
125
 
 
126
        gchar *cType = g_key_file_get_value (pKeyFile, "Desktop Entry", "Type", NULL);
 
127
        //g_print ("  cType : %s\n", cType);
 
128
        if (strncmp (cType, "Link", 4) != 0 && strncmp (cType, "FSDevice", 8) != 0)
 
129
        {
 
130
                g_free(cType);
 
131
                g_key_file_free (pKeyFile);
 
132
                return FALSE;
 
133
        }
 
134
        g_free(cType);
 
135
 
 
136
        *cName = g_key_file_get_string (pKeyFile, "Desktop Entry", "Name", NULL);
 
137
 
 
138
        *cURI = g_key_file_get_string (pKeyFile, "Desktop Entry", "URL", NULL);
 
139
 
 
140
        *cIconName = g_key_file_get_string (pKeyFile, "Desktop Entry", "Icon", NULL);
 
141
 
 
142
        *iVolumeID = g_key_file_get_integer (pKeyFile, "Desktop Entry", "X-Gnome-Drive", NULL);
 
143
 
 
144
        *bIsDirectory = TRUE;
 
145
 
 
146
        g_key_file_free (pKeyFile);
 
147
        return TRUE;
 
148
}
 
149
 
 
150
void vfs_backend_get_file_info (const gchar *cBaseURI, gchar **cName, gchar **cURI, gchar **cIconName, gboolean *bIsDirectory, int *iVolumeID, double *fOrder, CairoDockFMSortType iSortType)
 
151
{
 
152
        GError *erreur = NULL;
 
153
        g_return_if_fail (cBaseURI != NULL);
 
154
        cd_message ("%s (%s)", __func__, cBaseURI);
 
155
 
 
156
        ThunarVfsPath *pThunarPath = thunar_vfs_path_new(cBaseURI, &erreur);
 
157
        if (erreur != NULL)
 
158
        {
 
159
                cd_warning ("Attention : couldn't read %s (%s)", cBaseURI, erreur->message);
 
160
                g_error_free (erreur);
 
161
                return;
 
162
        }
 
163
        
 
164
        // distinguer les mounts points du reste
 
165
        ThunarVfsVolume *pThunarVolume = thunar_find_volume_from_path (pThunarPath);
 
166
        if (pThunarVolume != NULL)
 
167
                cd_message (" correspond a un volume");
 
168
        
 
169
        ThunarVfsInfo *pThunarVfsInfo = thunar_vfs_info_new_for_path(pThunarPath, &erreur);
 
170
        thunar_vfs_path_unref(pThunarPath);
 
171
        if (erreur != NULL)
 
172
        {
 
173
                /* Si on a trouve un volume, le chemin peut ne pas exister et donc cette erreur peut etre acceptable */
 
174
                if (pThunarVolume == NULL)
 
175
                {
 
176
                        cd_warning ("Attention : %s", erreur->message);
 
177
                        g_error_free (erreur);
 
178
                        thunar_vfs_info_unref(pThunarVfsInfo);
 
179
                        return;
 
180
                }
 
181
                g_error_free (erreur);
 
182
                erreur = NULL;
 
183
        }
 
184
 
 
185
    *fOrder = 0;
 
186
    if( pThunarVfsInfo )
 
187
    {
 
188
        if (iSortType == CAIRO_DOCK_FM_SORT_BY_DATE)
 
189
            *fOrder = (double)pThunarVfsInfo->mtime;
 
190
        else if (iSortType == CAIRO_DOCK_FM_SORT_BY_SIZE)
 
191
            *fOrder = (double)pThunarVfsInfo->size;
 
192
        else if (iSortType == CAIRO_DOCK_FM_SORT_BY_TYPE)
 
193
            *fOrder = (double)pThunarVfsInfo->type;
 
194
    }
 
195
 
 
196
        *cURI = g_strdup (cBaseURI);
 
197
 
 
198
    if( pThunarVolume )
 
199
    {
 
200
        *cName = g_strdup(thunar_vfs_volume_get_name( pThunarVolume ));
 
201
        *iVolumeID = 1;
 
202
        *bIsDirectory = FALSE;
 
203
        *cIconName = g_strdup(thunar_vfs_volume_lookup_icon_name(pThunarVolume, gtk_icon_theme_get_default()));
 
204
    }
 
205
    else if( pThunarVfsInfo )
 
206
    {
 
207
        *cName = g_strdup (pThunarVfsInfo->display_name);
 
208
        *iVolumeID = 0;
 
209
        *bIsDirectory = ((pThunarVfsInfo->type & THUNAR_VFS_FILE_TYPE_DIRECTORY) != 0);
 
210
        *cIconName = pThunarVfsInfo->custom_icon?g_strdup(pThunarVfsInfo->custom_icon):NULL;
 
211
 
 
212
        ThunarVfsMimeInfo*pThunarMimeInfo = pThunarVfsInfo->mime_info;
 
213
        if( pThunarMimeInfo )
 
214
        {
 
215
            const gchar *cMimeType = thunar_vfs_mime_info_get_name (pThunarMimeInfo);
 
216
            cd_message ("  cMimeType : %s", cMimeType);
 
217
            if ( *cIconName == NULL && cMimeType && strcmp (cMimeType, "application/x-desktop") == 0)
 
218
            {
 
219
                thunar_vfs_info_unref(pThunarVfsInfo);
 
220
                thunar_vfs_mime_info_unref( pThunarMimeInfo );
 
221
                file_manager_get_file_info_from_desktop_link (cBaseURI, cName, cURI, cIconName, bIsDirectory, iVolumeID);
 
222
                *fOrder = 0;
 
223
                return ;
 
224
            }
 
225
 
 
226
            /*On verra un peu plus tard pour les vignettes des images, hein*/
 
227
            if(*cIconName == NULL && (strncmp (cMimeType, "image", 5) == 0))
 
228
            {
 
229
                gchar *cHostname = NULL;
 
230
                gchar *cFilePath = g_filename_from_uri (cBaseURI, &cHostname, &erreur);
 
231
                if (erreur != NULL)
 
232
                {
 
233
                    g_error_free (erreur);
 
234
                }
 
235
                else if (cHostname == NULL || strcmp (cHostname, "localhost") == 0)  // on ne recupere la vignette que sur les fichiers locaux.
 
236
                {
 
237
                    *cIconName = thunar_vfs_path_dup_string(pThunarPath);
 
238
                    cairo_dock_remove_html_spaces (*cIconName);
 
239
                }
 
240
                g_free (cHostname);
 
241
            }
 
242
 
 
243
            if (*cIconName == NULL)
 
244
            {
 
245
                *cIconName = g_strdup (thunar_vfs_mime_info_lookup_icon_name(pThunarMimeInfo, gtk_icon_theme_get_default()));
 
246
            }
 
247
        }
 
248
    }
 
249
 
 
250
    if( pThunarVfsInfo )
 
251
    {
 
252
        thunar_vfs_info_unref(pThunarVfsInfo);
 
253
    }
 
254
}
 
255
 
 
256
 
 
257
struct ThunarFolder_t {
 
258
        GList *file_list;
 
259
        ThunarVfsJob *job;
 
260
        gboolean isJobFinished;
 
261
};
 
262
 
 
263
typedef struct ThunarFolder_t ThunarFolder;
 
264
 
 
265
static gboolean thunar_folder_infos_ready (ThunarVfsJob *job,
 
266
        GList        *infos,
 
267
        ThunarFolder *folder)
 
268
{
 
269
  /* merge the list with the existing list of new files */
 
270
  folder->file_list = g_list_concat (folder->file_list, infos);
 
271
 
 
272
  /* TRUE to indicate that we took over ownership of the infos list */
 
273
  return TRUE;
 
274
}
 
275
 
 
276
static void thunar_folder_finished (ThunarVfsJob *job, ThunarFolder *folder)
 
277
{
 
278
        /* we did it, the folder is loaded */
 
279
        g_signal_handlers_disconnect_matched (folder->job, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, folder);
 
280
        g_object_unref (G_OBJECT (folder->job));
 
281
        folder->job = NULL;
 
282
        folder->isJobFinished = TRUE;
 
283
}
 
284
 
 
285
/*
 
286
 * Attention: si l'URI demandee est CAIRO_DOCK_FM_VFS_ROOT, alors il faut retourner la liste des volumes !
 
287
 *    En effet dans ThunarVFS "root://" correspond simplement Ć  "/"
 
288
 */
 
289
GList *vfs_backend_list_directory (const gchar *cBaseURI, CairoDockFMSortType iSortType, int iNewIconsType, gboolean bListHiddenFiles, gchar **cFullURI)
 
290
{
 
291
        GError *erreur = NULL;
 
292
        g_return_val_if_fail (cBaseURI != NULL, NULL);
 
293
        cd_message ("%s (%s)", __func__, cBaseURI);
 
294
 
 
295
        GList *pIconList = NULL;
 
296
 
 
297
        const gchar *cURI = NULL;
 
298
        if (strcmp (cBaseURI, CAIRO_DOCK_FM_VFS_ROOT) == 0)
 
299
        {
 
300
                cURI = CAIRO_DOCK_FM_VFS_ROOT;
 
301
 
 
302
                if( cFullURI )
 
303
                {
 
304
                    *cFullURI = g_strdup(cURI);
 
305
                }
 
306
 
 
307
                /* listons joyeusement les volumes */
 
308
                ThunarVfsVolumeManager *pThunarVolumeManager = thunar_vfs_volume_manager_get_default();
 
309
                const GList *pListVolumes = thunar_vfs_volume_manager_get_volumes(pThunarVolumeManager);
 
310
                int lVolumeFakeID = 1;
 
311
 
 
312
                for( ; pListVolumes != NULL; pListVolumes = pListVolumes->next, lVolumeFakeID++ )
 
313
                {
 
314
                        ThunarVfsVolume *pThunarVfsVolume = (ThunarVfsVolume *)pListVolumes->data;
 
315
 
 
316
            /* Skip the volumes that are not there or that are not removable */
 
317
            if (!thunar_vfs_volume_is_present (pThunarVfsVolume) || !thunar_vfs_volume_is_removable (pThunarVfsVolume))
 
318
                continue;
 
319
 
 
320
                ThunarVfsPath *pThunarVfsPath = thunar_vfs_volume_get_mount_point(pThunarVfsVolume);
 
321
                        Icon *icon = g_new0 (Icon, 1);
 
322
 
 
323
                        /* il nous faut: URI, type, nom, icone */
 
324
 
 
325
                        icon->cBaseURI = thunar_vfs_path_dup_uri(pThunarVfsPath);
 
326
                        cd_debug ("mount point : %s", icon->cBaseURI);
 
327
 
 
328
                        icon->acCommand = thunar_vfs_path_dup_uri(pThunarVfsPath);
 
329
                        icon->iVolumeID = lVolumeFakeID;
 
330
 
 
331
                        cd_message (" -> icon->cBaseURI : %s", icon->cBaseURI);
 
332
 
 
333
                        icon->iType = iNewIconsType;
 
334
 
 
335
                        icon->acName = g_strdup(thunar_vfs_volume_get_name( pThunarVfsVolume ));
 
336
                        cd_debug (" -> icon->acName : %s", icon->acName);
 
337
 
 
338
                        icon->acFileName = g_strdup(thunar_vfs_volume_lookup_icon_name(pThunarVfsVolume, gtk_icon_theme_get_default()));
 
339
                        cd_debug (" -> icon->acFileName : %s", icon->acFileName);
 
340
 
 
341
                        erreur = NULL;
 
342
                    ThunarVfsInfo *pThunarVfsInfo = thunar_vfs_info_new_for_path(pThunarVfsPath, &erreur);
 
343
                        if (erreur != NULL)
 
344
                        {
 
345
                                icon->fOrder = 0;
 
346
                                cd_warning ("Attention : %s", erreur->message);
 
347
                                g_error_free (erreur);
 
348
                        }
 
349
                        else
 
350
                        {
 
351
                                if (iSortType == CAIRO_DOCK_FM_SORT_BY_SIZE)
 
352
                                        icon->fOrder = pThunarVfsInfo->size;
 
353
                                else if (iSortType == CAIRO_DOCK_FM_SORT_BY_DATE)
 
354
                                        icon->fOrder = pThunarVfsInfo->mtime;
 
355
                                else if (iSortType == CAIRO_DOCK_FM_SORT_BY_TYPE)
 
356
                                        icon->fOrder = pThunarVfsInfo->type;
 
357
 
 
358
                                thunar_vfs_info_unref(pThunarVfsInfo);
 
359
                        }
 
360
 
 
361
                        pIconList = g_list_prepend (pIconList, icon);
 
362
                }
 
363
 
 
364
                return pIconList;
 
365
        }
 
366
        else if (strcmp (cBaseURI, CAIRO_DOCK_FM_NETWORK) == 0)
 
367
        {
 
368
                cd_message (" -> Good try, but no, there's no network management in Thunar VFS !");
 
369
 
 
370
            if( cFullURI )
 
371
                *cFullURI = g_strdup(CAIRO_DOCK_FM_NETWORK);
 
372
 
 
373
                return pIconList;
 
374
        }
 
375
        else
 
376
                cURI = cBaseURI;
 
377
 
 
378
    if( cFullURI )
 
379
        *cFullURI = g_strdup(cURI);
 
380
        g_return_val_if_fail (cURI != NULL, NULL);
 
381
 
 
382
        cd_message (" -> cURI : %s", cURI);
 
383
 
 
384
        ThunarFolder *folder = g_new( ThunarFolder, 1 );
 
385
        folder->isJobFinished = FALSE;
 
386
        folder->file_list = NULL;
 
387
 
 
388
        ThunarVfsPath *pThunarPath = thunar_vfs_path_new(cURI, &erreur);
 
389
        if (erreur != NULL)
 
390
        {
 
391
                cd_warning ("couldn't read %s (%s)", cURI, erreur->message);
 
392
                g_error_free (erreur);
 
393
                return NULL;
 
394
        }
 
395
        folder->job = thunar_vfs_listdir(pThunarPath, NULL);
 
396
        thunar_vfs_path_unref(pThunarPath);
 
397
 
 
398
        g_signal_connect (folder->job, "error", G_CALLBACK (thunar_folder_finished), folder);
 
399
        g_signal_connect (folder->job, "finished", G_CALLBACK (thunar_folder_finished), folder);
 
400
        g_signal_connect (folder->job, "infos-ready", G_CALLBACK (thunar_folder_infos_ready), folder);
 
401
 
 
402
        while ( !folder->isJobFinished )
 
403
    {
 
404
      if (gtk_events_pending())
 
405
      {
 
406
          gtk_main_iteration(); // Handle unprocessed GTK events
 
407
      }
 
408
      else
 
409
      {
 
410
          g_thread_yield();     // Yield processing time
 
411
      }
 
412
    }
 
413
 
 
414
        /* Ć” joie, on a une liste de ThunarVfsInfos ! */
 
415
        GList *lp = NULL;
 
416
        for (lp = folder->file_list; lp != NULL; lp = lp->next)
 
417
    {
 
418
                /* get the info... */
 
419
                ThunarVfsInfo *pThunarVfsInfo = (ThunarVfsInfo *)(lp->data);
 
420
 
 
421
                Icon *icon;
 
422
 
 
423
                if(pThunarVfsInfo != NULL &&
 
424
                   strcmp (thunar_vfs_path_get_name(pThunarVfsInfo->path), ".") != 0 &&
 
425
                   strcmp (thunar_vfs_path_get_name(pThunarVfsInfo->path), "..") != 0)
 
426
                {
 
427
                        icon = g_new0 (Icon, 1);
 
428
                        icon->cBaseURI = thunar_vfs_path_dup_uri(pThunarVfsInfo->path);
 
429
                        cd_message (" item in directory : %s", icon->cBaseURI);
 
430
                        icon->iType = iNewIconsType;
 
431
                        if ( strcmp (thunar_vfs_mime_info_get_name(pThunarVfsInfo->mime_info), "application/x-desktop") == 0)
 
432
                        {
 
433
                                gboolean bIsDirectory = FALSE;
 
434
                                file_manager_get_file_info_from_desktop_link (icon->cBaseURI, &icon->acName, &icon->acCommand, &icon->acFileName, &bIsDirectory, &icon->iVolumeID);
 
435
                                cd_message ("  bIsDirectory : %d; iVolumeID : %d", bIsDirectory, icon->iVolumeID);
 
436
                        }
 
437
                        else
 
438
                        {
 
439
                                icon->acCommand = g_strdup(icon->cBaseURI);
 
440
                                icon->acName = g_strdup (thunar_vfs_path_get_name(pThunarVfsInfo->path));
 
441
                                icon->acFileName = NULL;
 
442
                                if (strncmp (thunar_vfs_mime_info_get_name(pThunarVfsInfo->mime_info), "image", 5) == 0)  // && strncmp (cFileURI, "file://", 7) == 0
 
443
                                {
 
444
                                        gchar *cHostname = NULL;
 
445
                                        GError *erreur = NULL;
 
446
                                        gchar *cFilePath = g_filename_from_uri (icon->cBaseURI, &cHostname, &erreur);
 
447
                                        if (erreur != NULL)
 
448
                                        {
 
449
                                                g_error_free (erreur);
 
450
                                                erreur = NULL;
 
451
                                        }
 
452
                                        else if (cHostname == NULL || strcmp (cHostname, "localhost") == 0)  // on ne recupere la vignette que sur les fichiers locaux.
 
453
                                        {
 
454
                                                icon->acFileName = g_strdup (cFilePath);
 
455
                                                cairo_dock_remove_html_spaces (icon->acFileName);
 
456
                                        }
 
457
                                        g_free (cHostname);
 
458
                                }
 
459
                                if (icon->acFileName == NULL)
 
460
                                {
 
461
                                        icon->acFileName = g_strdup(thunar_vfs_mime_info_lookup_icon_name(pThunarVfsInfo->mime_info, gtk_icon_theme_get_default()));
 
462
                                }
 
463
                        }
 
464
 
 
465
                        if (iSortType == CAIRO_DOCK_FM_SORT_BY_SIZE)
 
466
                                icon->fOrder = pThunarVfsInfo->size;
 
467
                        else if (iSortType == CAIRO_DOCK_FM_SORT_BY_DATE)
 
468
                                icon->fOrder = pThunarVfsInfo->mtime;
 
469
                        else if (iSortType == CAIRO_DOCK_FM_SORT_BY_TYPE)
 
470
                                icon->fOrder = pThunarVfsInfo->type;
 
471
 
 
472
                        pIconList = g_list_prepend (pIconList, icon);
 
473
                }
 
474
 
 
475
                /* ...release the info at the list position... */
 
476
                thunar_vfs_info_unref (lp->data);
 
477
        }
 
478
 
 
479
        if (iSortType == CAIRO_DOCK_FM_SORT_BY_NAME)
 
480
                pIconList = cairo_dock_sort_icons_by_name (pIconList);
 
481
        else
 
482
                pIconList = cairo_dock_sort_icons_by_order (pIconList);
 
483
 
 
484
        return pIconList;
 
485
}
 
486
 
 
487
ThunarVfsVolume *thunar_find_volume_from_path (ThunarVfsPath *pThunarPath)
 
488
{
 
489
        GError *erreur = NULL;
 
490
        gchar *ltmp_path = NULL;
 
491
        
 
492
        ThunarVfsVolume *pThunarVolume = NULL;
 
493
        
 
494
        /* premiere methode: on scanne les volumes. c'est peut-etre un volume non monte... */
 
495
        ThunarVfsVolumeManager *pThunarVolumeManager = thunar_vfs_volume_manager_get_default();
 
496
        GList *pListVolumes = thunar_vfs_volume_manager_get_volumes(pThunarVolumeManager);
 
497
        for( ; pListVolumes != NULL; pListVolumes = pListVolumes->next )
 
498
        {
 
499
                pThunarVolume = (ThunarVfsVolume *)pListVolumes->data;
 
500
                ThunarVfsPath *pMountPointVfsPath = thunar_vfs_volume_get_mount_point(pThunarVolume);
 
501
                ltmp_path = thunar_vfs_path_dup_uri(pMountPointVfsPath);
 
502
                if( ltmp_path )
 
503
                {
 
504
                        cd_debug (" - %s", ltmp_path);
 
505
                        g_free(ltmp_path);
 
506
                }
 
507
                
 
508
                /* Skip the volumes that are not there or that are not removable */
 
509
                if (!thunar_vfs_volume_is_present (pThunarVolume) || !thunar_vfs_volume_is_removable (pThunarVolume))
 
510
                {
 
511
                        pThunarVolume = NULL;
 
512
                        cd_debug (" saute");
 
513
                        continue;
 
514
                }
 
515
        
 
516
                
 
517
                if( thunar_vfs_path_equal(pThunarPath, pMountPointVfsPath) )  // || thunar_vfs_path_is_ancestor( pThunarPath, pMountPointVfsPath )
 
518
                {
 
519
                        cd_debug (" trouve !");
 
520
                        break;
 
521
                }
 
522
                pThunarVolume = NULL;
 
523
        }
 
524
        
 
525
        /* if( pThunarVolume == NULL )
 
526
        {
 
527
                // deuxieme methode: avec le vfs_info. 
 
528
                ThunarVfsInfo *pThunarVfsInfo = thunar_vfs_info_new_for_path(pThunarPath, &erreur);
 
529
                if (erreur != NULL)
 
530
                {
 
531
                        cd_warning ("Attention : %s", erreur->message);
 
532
                        g_error_free (erreur);
 
533
                }
 
534
                else
 
535
                {
 
536
                        ThunarVfsVolumeManager *pThunarVolumeManager = thunar_vfs_volume_manager_get_default();
 
537
                        pThunarVolume = thunar_vfs_volume_manager_get_volume_by_info(pThunarVolumeManager, pThunarVfsInfo);
 
538
                        g_object_unref(pThunarVolumeManager);
 
539
                        thunar_vfs_info_unref(pThunarVfsInfo);
 
540
                        g_print ("2eme methode -> volume : %x\n", pThunarVolume);
 
541
        
 
542
                        // Skip the volumes that are not there or that are not removable 
 
543
                        if (pThunarVolume == NULL || !thunar_vfs_volume_is_present (pThunarVolume) || !thunar_vfs_volume_is_removable (pThunarVolume))
 
544
                        {
 
545
                        pThunarVolume = NULL;
 
546
                        }
 
547
                }
 
548
        }*/
 
549
        
 
550
        return pThunarVolume;
 
551
}
 
552
 
 
553
/* Fait */
 
554
void vfs_backend_launch_uri (const gchar *cURI)
 
555
{
 
556
        GError *erreur = NULL;
 
557
        g_return_if_fail (cURI != NULL);
 
558
        cd_message ("%s (%s)", __func__, cURI);
 
559
 
 
560
        ThunarVfsPath *pThunarPath = thunar_vfs_path_new(cURI, &erreur);
 
561
        if (erreur != NULL)
 
562
        {
 
563
                cd_warning ("Attention : couldn't read %s (%s)", cURI, erreur->message);
 
564
                g_error_free (erreur);
 
565
                return;
 
566
        }
 
567
 
 
568
        ThunarVfsPath *pThunarRealPath = NULL;
 
569
 
 
570
    /* hop, trouvons le volume correspondant */
 
571
        ThunarVfsVolume *pThunarVolume = thunar_find_volume_from_path(pThunarPath);
 
572
        if (pThunarVolume != NULL)
 
573
        {
 
574
                thunar_vfs_path_unref(pThunarPath);
 
575
                pThunarPath = thunar_vfs_volume_get_mount_point(pThunarVolume);
 
576
        }
 
577
 
 
578
    ThunarVfsInfo *pThunarVfsInfo = thunar_vfs_info_new_for_path(pThunarPath, &erreur);
 
579
        if (erreur != NULL)
 
580
        {
 
581
                cd_warning ("Attention : %s", erreur->message);
 
582
                g_error_free (erreur);
 
583
                return;
 
584
        }
 
585
 
 
586
        /* if this is a directory, open Thunar in that directory */
 
587
        if( pThunarVfsInfo->flags & THUNAR_VFS_FILE_FLAGS_EXECUTABLE )
 
588
        {
 
589
                thunar_vfs_info_execute(pThunarVfsInfo,NULL,NULL, NULL,&erreur);
 
590
                if (erreur != NULL)
 
591
                {
 
592
                        cd_warning ("Attention : %s", erreur->message);
 
593
                        g_error_free (erreur);
 
594
                }
 
595
        }
 
596
        else
 
597
        {
 
598
                ThunarVfsMimeDatabase *pMimeDB = thunar_vfs_mime_database_get_default();
 
599
                if( pMimeDB )
 
600
                {
 
601
                        ThunarVfsMimeApplication *pMimeApplication = thunar_vfs_mime_database_get_default_application(pMimeDB, pThunarVfsInfo->mime_info);
 
602
                        if( pMimeApplication )
 
603
                        {
 
604
                                GList *path_list = g_list_prepend (NULL, pThunarPath);
 
605
                                cd_message ("Launching %s ...", thunar_vfs_mime_handler_get_command(pMimeApplication) );
 
606
                                thunar_vfs_mime_handler_exec(pMimeApplication,gdk_screen_get_default (),path_list,&erreur);
 
607
                                g_list_free(path_list);
 
608
                                g_object_unref( pMimeApplication );
 
609
                                if (erreur != NULL)
 
610
                                {
 
611
                                        cd_warning ("Attention : %s", erreur->message);
 
612
                                        g_error_free (erreur);
 
613
                                }
 
614
                        }
 
615
                        g_object_unref( pMimeDB );
 
616
                }
 
617
        }
 
618
 
 
619
        thunar_vfs_info_unref(pThunarVfsInfo);
 
620
}
 
621
 
 
622
/* Fait */
 
623
gchar *vfs_backend_is_mounted (const gchar *cURI, gboolean *bIsMounted)
 
624
{
 
625
        GError *erreur = NULL;
 
626
        cd_message ("%s (%s)", __func__, cURI);
 
627
 
 
628
    ThunarVfsVolume *pThunarVolume = NULL;
 
629
 
 
630
        ThunarVfsPath *pThunarPath = thunar_vfs_path_new(cURI, &erreur);
 
631
        if (erreur != NULL)
 
632
        {
 
633
        cd_warning ("ERROR : %s", erreur->message);
 
634
                g_error_free (erreur);
 
635
                return NULL;
 
636
        }
 
637
 
 
638
    /* hop, trouvons le volume correspondant */
 
639
    pThunarVolume = thunar_find_volume_from_path(pThunarPath);
 
640
    thunar_vfs_path_unref(pThunarPath);
 
641
 
 
642
        if (pThunarVolume == NULL)
 
643
        {
 
644
                cd_warning ("Attention : no volume associated to %s, we'll assume that it is not mounted", cURI);
 
645
                *bIsMounted = FALSE;
 
646
                return NULL;
 
647
        }
 
648
 
 
649
        *bIsMounted = thunar_vfs_volume_is_mounted(pThunarVolume);
 
650
        ThunarVfsPath *pMountPointVfsPath = thunar_vfs_volume_get_mount_point(pThunarVolume);
 
651
        gchar *cMountPointID = pMountPointVfsPath?thunar_vfs_path_dup_uri(pMountPointVfsPath):NULL;
 
652
 
 
653
        cd_message ("  bIsMounted <- %d", *bIsMounted);
 
654
 
 
655
        return cMountPointID;
 
656
}
 
657
 
 
658
 
 
659
static void _vfs_backend_mount_callback(ThunarVfsVolume *volume, gpointer *data)
 
660
{
 
661
        cd_message ("%s (%x)", __func__, data);
 
662
        
 
663
        /// Debug.
 
664
        ThunarVfsVolumeManager *pThunarVolumeManager = thunar_vfs_volume_manager_get_default();
 
665
        GList *pListVolumes = thunar_vfs_volume_manager_get_volumes(pThunarVolumeManager);
 
666
        for( ; pListVolumes != NULL; pListVolumes = pListVolumes->next )
 
667
        {
 
668
                ThunarVfsVolume *pThunarVolume = (ThunarVfsVolume *)pListVolumes->data;
 
669
                /* Skip the volumes that are not there or that are not removable */
 
670
                if (!thunar_vfs_volume_is_present (pThunarVolume) || !thunar_vfs_volume_is_removable (pThunarVolume))
 
671
                {
 
672
                        pThunarVolume = NULL;
 
673
                        continue;
 
674
                }
 
675
                ThunarVfsPath *pMountPointVfsPath = thunar_vfs_volume_get_mount_point(pThunarVolume);
 
676
                gchar *ltmp_path = thunar_vfs_path_dup_uri(pMountPointVfsPath);
 
677
                cd_debug (" + %s", ltmp_path);
 
678
        }
 
679
        
 
680
        CairoDockFMMountCallback pCallback = data[0];
 
681
        pCallback (GPOINTER_TO_INT (data[1]), TRUE, data[2], data[3], data[4]);
 
682
}
 
683
/*static void _vfs_backend_change_callback(ThunarVfsVolume *volume, gpointer *data)
 
684
{
 
685
        cd_message ("%s (%x)", __func__, data);
 
686
        ThunarVfsVolumeManager *pThunarVolumeManager = thunar_vfs_volume_manager_get_default();
 
687
        GList *pListVolumes = thunar_vfs_volume_manager_get_volumes(pThunarVolumeManager);
 
688
        for( ; pListVolumes != NULL; pListVolumes = pListVolumes->next )
 
689
        {
 
690
                ThunarVfsVolume *pThunarVolume = (ThunarVfsVolume *)pListVolumes->data;
 
691
        
 
692
                // Skip the volumes that are not there or that are not removable
 
693
                if (!thunar_vfs_volume_is_present (pThunarVolume) || !thunar_vfs_volume_is_removable (pThunarVolume))
 
694
                {
 
695
                        pThunarVolume = NULL;
 
696
                        continue;
 
697
                }
 
698
        
 
699
                ThunarVfsPath *pMountPointVfsPath = thunar_vfs_volume_get_mount_point(pThunarVolume);
 
700
                gchar *ltmp_path = thunar_vfs_path_dup_uri(pMountPointVfsPath);
 
701
                cd_debug (" + %s", ltmp_path);
 
702
        }
 
703
        CairoDockFMMountCallback pCallback = data[0];
 
704
 
 
705
        pCallback (GPOINTER_TO_INT (data[1]), TRUE, data[2], data[3], data[4]);
 
706
}*/
 
707
 
 
708
void vfs_backend_mount (const gchar *cURI, int iVolumeID, CairoDockFMMountCallback pCallback, Icon *icon, CairoContainer *pContainer)
 
709
{
 
710
        GError *erreur = NULL;
 
711
        g_return_if_fail (cURI != NULL);
 
712
        cd_message ("%s (%s)", __func__, cURI);
 
713
 
 
714
        ThunarVfsPath *pThunarPath = thunar_vfs_path_new(cURI, &erreur);
 
715
        if (erreur != NULL)
 
716
        {
 
717
                cd_warning ("Attention : couldn't read %s (%s)", cURI, erreur->message);
 
718
                g_error_free (erreur);
 
719
                return;
 
720
        }
 
721
    /* hop, trouvons le volume correspondant */
 
722
    ThunarVfsVolume *pThunarVolume = thunar_find_volume_from_path(pThunarPath);
 
723
    thunar_vfs_path_unref(pThunarPath);
 
724
 
 
725
        if (pThunarVolume == NULL)
 
726
        {
 
727
                cd_warning ("Attention : no volume associated to %s", cURI);
 
728
                return;
 
729
        }
 
730
 
 
731
        gpointer *data2 = g_new (gpointer, 5);
 
732
        data2[0] = pCallback;
 
733
        data2[1] = GINT_TO_POINTER (TRUE);
 
734
        data2[2] = thunar_vfs_volume_get_name(pThunarVolume);
 
735
        data2[3] = icon;
 
736
        data2[4] = pContainer;
 
737
        g_signal_connect(pThunarVolume, "mounted", G_CALLBACK (_vfs_backend_mount_callback), data2);
 
738
 
 
739
        if( !thunar_vfs_volume_mount(pThunarVolume, NULL, &erreur) )
 
740
        {
 
741
                cd_warning ("Attention, %s couldn't be mounted : %s",cURI, erreur->message);
 
742
                g_error_free (erreur);
 
743
        }
 
744
        g_signal_handlers_disconnect_matched (pThunarVolume, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, data2);
 
745
        g_free (data2);
 
746
}
 
747
 
 
748
void vfs_backend_unmount (const gchar *cURI, int iVolumeID, CairoDockFMMountCallback pCallback, Icon *icon, CairoContainer *pContainer)
 
749
{
 
750
        GError *erreur = NULL;
 
751
        g_return_if_fail (cURI != NULL);
 
752
        cd_message ("%s (%s)", __func__, cURI);
 
753
 
 
754
        ThunarVfsPath *pThunarPath = thunar_vfs_path_new(cURI, &erreur);
 
755
        if (erreur != NULL)
 
756
        {
 
757
                cd_message ("Attention : couldn't read %s (%s)", cURI, erreur->message);
 
758
                g_error_free (erreur);
 
759
                return;
 
760
        }
 
761
    /* hop, trouvons le volume correspondant */
 
762
    ThunarVfsVolume *pThunarVolume = thunar_find_volume_from_path(pThunarPath);
 
763
    thunar_vfs_path_unref(pThunarPath);
 
764
 
 
765
        if (pThunarVolume == NULL)
 
766
        {
 
767
                cd_warning ("Attention : no volume associated to %s", cURI);
 
768
                return ;
 
769
        }
 
770
 
 
771
        gpointer *data2 = g_new (gpointer, 5);
 
772
        data2[0] = pCallback;
 
773
        data2[1] = GINT_TO_POINTER (FALSE);
 
774
        data2[2] = thunar_vfs_volume_get_name(pThunarVolume);
 
775
        data2[3] = icon;
 
776
        data2[4] = pContainer;
 
777
        g_signal_connect(pThunarVolume, "unmounted", G_CALLBACK (_vfs_backend_mount_callback), data2);
 
778
 
 
779
        if( !thunar_vfs_volume_unmount(pThunarVolume, NULL, &erreur) )
 
780
        {
 
781
                cd_message ("Attention, %s couldn't be unmounted : %s\n",cURI, erreur->message);
 
782
                g_error_free (erreur);
 
783
        }
 
784
        cd_debug ("demontage fini");
 
785
        g_signal_handlers_disconnect_matched (pThunarVolume, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, data2);
 
786
        g_free (data2);
 
787
}
 
788
 
 
789
static void _vfs_backend_volume_added_callback (ThunarVfsVolumeManager *manager,
 
790
                                                gpointer                volumes,
 
791
                                                gpointer                *data)
 
792
{
 
793
        CairoDockFMMonitorCallback pCallback = data[0];
 
794
        gpointer user_data = data[1];
 
795
        cd_message ("");
 
796
        
 
797
        /* call the callback for each volume */
 
798
        GList *pListVolumes = volumes;
 
799
        for( ; pListVolumes != NULL; pListVolumes = pListVolumes->next )
 
800
        {
 
801
                ThunarVfsVolume *pThunarVolume = (ThunarVfsVolume *)pListVolumes->data;
 
802
        
 
803
                ThunarVfsPath *pMountPointVfsPath = thunar_vfs_volume_get_mount_point(pThunarVolume);
 
804
                gchar *info_uri = thunar_vfs_path_dup_uri(pMountPointVfsPath);
 
805
        
 
806
                pCallback (CAIRO_DOCK_FILE_CREATED, info_uri, user_data);
 
807
                g_free(info_uri);
 
808
        }
 
809
}
 
810
 
 
811
static void _vfs_backend_volume_removed_callback (ThunarVfsVolumeManager *manager,
 
812
                                                gpointer                volumes,
 
813
                                                gpointer                *data)
 
814
{
 
815
        CairoDockFMMonitorCallback pCallback = data[0];
 
816
        gpointer user_data = data[1];
 
817
        cd_message ("");
 
818
 
 
819
        /* call the callback for each volume */
 
820
    GList *pListVolumes = volumes;
 
821
    for( ; pListVolumes != NULL; pListVolumes = pListVolumes->next )
 
822
    {
 
823
        ThunarVfsVolume *pThunarVolume = (ThunarVfsVolume *)pListVolumes->data;
 
824
 
 
825
        ThunarVfsPath *pMountPointVfsPath = thunar_vfs_volume_get_mount_point(pThunarVolume);
 
826
        gchar *info_uri = thunar_vfs_path_dup_uri(pMountPointVfsPath);
 
827
 
 
828
        pCallback (CAIRO_DOCK_FILE_DELETED, info_uri, user_data);
 
829
        g_free(info_uri);
 
830
    }
 
831
}
 
832
 
 
833
/*void _vfs_backend_volume_modified_callback (ThunarVfsVolumeManager *manager, ThunarVfsVolume *pVolume, gpointer *data)
 
834
{
 
835
        CairoDockFMMonitorCallback pCallback = data[0];
 
836
        gpointer user_data = data[1];
 
837
        cd_message ("");
 
838
        /// On a le volume tel qu'il est apres le montage/demontage. Du coup, son URI est la nouvelle. Or la modification se fait sur l'ancienne ...
 
839
        
 
840
        gboolean bMounted = thunar_vfs_volume_is_mounted(pVolume);
 
841
        if (bMounted)  // on vient de le monter. avant c'etait /dev/sdb1, maintenant c'est /media/disk
 
842
        {
 
843
                const gchar* cName = thunar_vfs_volume_get_name (pVolume);
 
844
                cd_debug ("volume to remove : %s", cName);
 
845
                pCallback (CAIRO_DOCK_FILE_DELETED, cName, user_data);
 
846
                
 
847
                const ThunarVfsPath *pMountPoint = thunar_vfs_volume_get_mount_point (pVolume);
 
848
                gchar *cUri = thunar_vfs_path_dup_uri (pMountPoint);
 
849
                cd_debug ("new mount point : %s", cUri);
 
850
                pCallback (CAIRO_DOCK_FILE_CREATED, cUri, user_data);
 
851
                g_free (cUri);
 
852
        }
 
853
        else  // on vient de le demonter, avant c'etait /media/disk, mais maintenant aussi !
 
854
        {
 
855
                const ThunarVfsPath *pMountPoint = thunar_vfs_volume_get_mount_point (pVolume);
 
856
                gchar *cUri = thunar_vfs_path_dup_uri (pMountPoint);
 
857
                cd_debug ("mount point : %s", cUri);
 
858
                
 
859
                /// Debug.
 
860
                ThunarVfsVolumeManager *pThunarVolumeManager = thunar_vfs_volume_manager_get_default();
 
861
                GList *pListVolumes = thunar_vfs_volume_manager_get_volumes(pThunarVolumeManager);
 
862
                for( ; pListVolumes != NULL; pListVolumes = pListVolumes->next )
 
863
                {
 
864
                        ThunarVfsVolume *pThunarVolume = (ThunarVfsVolume *)pListVolumes->data;
 
865
                
 
866
                        // Skip the volumes that are not there or that are not removable.
 
867
                        if (!thunar_vfs_volume_is_present (pThunarVolume) || !thunar_vfs_volume_is_removable (pThunarVolume))
 
868
                        {
 
869
                                pThunarVolume = NULL;
 
870
                                continue;
 
871
                        }
 
872
                
 
873
                        ThunarVfsPath *pMountPointVfsPath = thunar_vfs_volume_get_mount_point(pThunarVolume);
 
874
                        gchar *ltmp_path = thunar_vfs_path_dup_uri(pMountPointVfsPath);
 
875
                        cd_debug (" + %s", ltmp_path);
 
876
                }
 
877
                
 
878
                pCallback (CAIRO_DOCK_FILE_MODIFIED, cUri, user_data);
 
879
                g_free (cUri);
 
880
        }
 
881
}*/
 
882
 
 
883
 
 
884
static void _vfs_backend_thunar_monitor_callback(ThunarVfsMonitor *monitor,
 
885
                                                 ThunarVfsMonitorHandle *handle,
 
886
                                                 ThunarVfsMonitorEvent event,
 
887
                                                 ThunarVfsPath *handle_path,
 
888
                                                 ThunarVfsPath *event_path,
 
889
                                                 gpointer *data)
 
890
{
 
891
        CairoDockFMMonitorCallback pCallback = data[0];
 
892
        gpointer user_data = data[1];
 
893
        cd_message ("%s (%d , data : %x)", __func__, event, user_data);
 
894
 
 
895
        CairoDockFMEventType iEventType;
 
896
        switch (event)
 
897
        {
 
898
                case THUNAR_VFS_MONITOR_EVENT_CHANGED :
 
899
                        iEventType = CAIRO_DOCK_FILE_MODIFIED;
 
900
                break;
 
901
 
 
902
                case THUNAR_VFS_MONITOR_EVENT_DELETED :
 
903
                        iEventType = CAIRO_DOCK_FILE_DELETED;
 
904
                break;
 
905
 
 
906
                case THUNAR_VFS_MONITOR_EVENT_CREATED :
 
907
                        iEventType = CAIRO_DOCK_FILE_CREATED;
 
908
                break;
 
909
 
 
910
                default :
 
911
                return ;
 
912
        }
 
913
        gchar *info_uri = thunar_vfs_path_dup_uri(event_path);
 
914
        pCallback (iEventType, info_uri, user_data);
 
915
        g_free(info_uri);
 
916
}
 
917
static void _vfs_backend_volume_changed_callback (ThunarVfsVolume *pThunarVolume, gpointer *data)
 
918
{
 
919
        CairoDockFMMonitorCallback pCallback = data[0];
 
920
        gpointer user_data = data[1];
 
921
        gchar *cUri = data[2];
 
922
        cd_debug ("%x - %x - %x", data[0], data[1], data[2]);
 
923
        g_return_if_fail (cUri != NULL);
 
924
        cd_message (" >>> %s (%s)", __func__, cUri);
 
925
        
 
926
        ThunarVfsPath *pMountPointVfsPath = thunar_vfs_volume_get_mount_point(pThunarVolume);
 
927
        gchar *cNewUri = thunar_vfs_path_dup_uri(pMountPointVfsPath);
 
928
        if (strcmp (cNewUri, cUri) != 0)
 
929
        {
 
930
                cd_message (" ce volume devient : %s", cNewUri);
 
931
                
 
932
                pCallback (CAIRO_DOCK_FILE_DELETED, cUri, user_data);
 
933
                
 
934
                pCallback (CAIRO_DOCK_FILE_CREATED, cNewUri, user_data);
 
935
                g_free (cUri);
 
936
                data[2] = cNewUri;
 
937
        }
 
938
        else
 
939
                g_free (cNewUri);
 
940
}
 
941
 
 
942
 
 
943
void vfs_backend_add_monitor (const gchar *cURI, gboolean bDirectory, CairoDockFMMonitorCallback pCallback, gpointer user_data)
 
944
{
 
945
        GError *erreur = NULL;
 
946
        g_return_if_fail (cURI != NULL);
 
947
        cd_message ("%s (%s)", __func__, cURI);
 
948
 
 
949
    // faire un gros cas particulier pour CAIRO_DOCK_FM_VFS_ROOT, qui est retournĆ© lors du listing dudit rĆ©pertoire
 
950
    if( strcmp( cURI, CAIRO_DOCK_FM_VFS_ROOT ) == 0 )
 
951
    {
 
952
                // se brancher sur les signaux "volumes-added" et "volumes-removed" du volume manager
 
953
                gpointer *data = g_new0 (gpointer, 2);
 
954
                data[0] = pCallback;
 
955
                data[1] = user_data;
 
956
        
 
957
                ThunarVfsVolumeManager *pThunarVolumeManager = thunar_vfs_volume_manager_get_default();
 
958
                // on nettoie d'abord, au cas ou
 
959
                g_signal_handlers_disconnect_matched (pThunarVolumeManager, G_SIGNAL_MATCH_FUNC, 0, 0, NULL, _vfs_backend_volume_added_callback, NULL);
 
960
                g_signal_handlers_disconnect_matched (pThunarVolumeManager, G_SIGNAL_MATCH_FUNC, 0, 0, NULL, _vfs_backend_volume_removed_callback, NULL);
 
961
                ///g_signal_handlers_disconnect_matched (pThunarVolumeManager, G_SIGNAL_MATCH_FUNC, 0, 0, NULL, _vfs_backend_volume_modified_callback, NULL);
 
962
                ///g_signal_handlers_disconnect_matched (pThunarVolumeManager, G_SIGNAL_MATCH_FUNC, 0, 0, NULL, _vfs_backend_volume_modified_callback, NULL);
 
963
                // puis on se branche
 
964
                g_signal_connect(pThunarVolumeManager, "volumes-added", G_CALLBACK (_vfs_backend_volume_added_callback), data);
 
965
                g_signal_connect(pThunarVolumeManager, "volumes-removed", G_CALLBACK (_vfs_backend_volume_removed_callback), data);
 
966
                ///g_signal_connect(pThunarVolumeManager, "volume-mounted", G_CALLBACK (_vfs_backend_volume_modified_callback), data);
 
967
                ///g_signal_connect(pThunarVolumeManager, "volume-unmounted", G_CALLBACK (_vfs_backend_volume_modified_callback), data);
 
968
                
 
969
                //\____________ Pour avoir les changements de point de montage, on se connecte au signal "changed" de chaque volume. En effet, le signal "unmounted" est emis juste apres le demontage, le changement de point de montage ne s'est pas encore fait !
 
970
                GList *pListVolumes = thunar_vfs_volume_manager_get_volumes(pThunarVolumeManager);
 
971
                for( ; pListVolumes != NULL; pListVolumes = pListVolumes->next )
 
972
                {
 
973
                        ThunarVfsVolume *pThunarVolume = (ThunarVfsVolume *)pListVolumes->data;
 
974
                
 
975
                        /* Skip the volumes that are not there or that are not removable */
 
976
                        if (!thunar_vfs_volume_is_present (pThunarVolume) || !thunar_vfs_volume_is_removable (pThunarVolume))
 
977
                        {
 
978
                                pThunarVolume = NULL;
 
979
                                continue;
 
980
                        }
 
981
                        
 
982
                        ThunarVfsPath *pMountPointVfsPath = thunar_vfs_volume_get_mount_point(pThunarVolume);
 
983
                        gchar *ltmp_path = thunar_vfs_path_dup_uri(pMountPointVfsPath);
 
984
                        cd_debug (" signal ajoute sur %s", ltmp_path);
 
985
                        gpointer *data2 = g_new0 (gpointer, 3);
 
986
                        data2[0] = pCallback;
 
987
                        data2[1] = user_data;
 
988
                        data2[2] = ltmp_path;
 
989
                        g_signal_connect(pThunarVolume, "changed", G_CALLBACK (_vfs_backend_volume_changed_callback), data2);
 
990
                        cd_debug ("%x - %x - %x", data2[0], data2[1], data2[2]);
 
991
                }
 
992
                return;
 
993
        }
 
994
 
 
995
        ThunarVfsPath *pThunarPath = thunar_vfs_path_new(cURI, &erreur);
 
996
        if (erreur != NULL)
 
997
        {
 
998
                cd_warning ("Attention : %s", erreur->message);
 
999
                g_error_free (erreur);
 
1000
                return;
 
1001
        }
 
1002
 
 
1003
        ThunarVfsMonitor *pThunarMonitor = thunar_vfs_monitor_get_default();
 
1004
        ThunarVfsMonitorHandle *pHandle = NULL;
 
1005
        gpointer *data = g_new0 (gpointer, 3);
 
1006
        data[0] = pCallback;
 
1007
        data[1] = user_data;
 
1008
 
 
1009
        if( bDirectory )
 
1010
        {
 
1011
                pHandle = thunar_vfs_monitor_add_directory(pThunarMonitor, pThunarPath,
 
1012
                                                                                                   (ThunarVfsMonitorCallback) _vfs_backend_thunar_monitor_callback,
 
1013
                                                                                                   data);
 
1014
        }
 
1015
        else
 
1016
        {
 
1017
                pHandle = thunar_vfs_monitor_add_file(pThunarMonitor, pThunarPath,
 
1018
                                                                                          (ThunarVfsMonitorCallback) _vfs_backend_thunar_monitor_callback,
 
1019
                                                                                          data);
 
1020
        }
 
1021
        g_object_unref(pThunarMonitor);
 
1022
        thunar_vfs_path_unref(pThunarPath);
 
1023
 
 
1024
        if (pHandle == NULL)
 
1025
        {
 
1026
                cd_warning ("Attention : couldn't add monitor function to %s\n  I will not be able to receive events about this file", cURI);
 
1027
                g_free (data);
 
1028
        }
 
1029
        else
 
1030
        {
 
1031
                cd_message (">>> moniteur ajoute sur %s (%x)", cURI, user_data);
 
1032
                data[2] = pHandle;
 
1033
                g_hash_table_insert (s_fm_MonitorHandleTable, g_strdup (cURI), data);
 
1034
        }
 
1035
}
 
1036
 
 
1037
void vfs_backend_remove_monitor (const gchar *cURI)
 
1038
{
 
1039
        cd_message ("%s (%s)", __func__, cURI);
 
1040
        if (cURI != NULL)
 
1041
        {
 
1042
                gpointer *data = g_hash_table_lookup(s_fm_MonitorHandleTable,cURI);
 
1043
                if( data )
 
1044
                {
 
1045
                        ThunarVfsMonitorHandle *pHandle = data[2];
 
1046
                        if( pHandle )
 
1047
                        {
 
1048
                                ThunarVfsMonitor *pThunarMonitor = thunar_vfs_monitor_get_default();
 
1049
                                thunar_vfs_monitor_remove(pThunarMonitor, pHandle);
 
1050
                                g_object_unref(pThunarMonitor);
 
1051
                        }
 
1052
                }
 
1053
 
 
1054
                cd_message (">>> moniteur supprime sur %s", cURI);
 
1055
                g_hash_table_remove (s_fm_MonitorHandleTable, cURI);
 
1056
                
 
1057
                if( strcmp( cURI, CAIRO_DOCK_FM_VFS_ROOT ) == 0 )
 
1058
                {
 
1059
                        ThunarVfsVolumeManager *pThunarVolumeManager = thunar_vfs_volume_manager_get_default();
 
1060
                        GList *pListVolumes = thunar_vfs_volume_manager_get_volumes(pThunarVolumeManager);
 
1061
                        for( ; pListVolumes != NULL; pListVolumes = pListVolumes->next )
 
1062
                        {
 
1063
                                ThunarVfsVolume *pThunarVolume = (ThunarVfsVolume *)pListVolumes->data;
 
1064
                        
 
1065
                                /* Skip the volumes that are not there or that are not removable */
 
1066
                                if (!thunar_vfs_volume_is_present (pThunarVolume) || !thunar_vfs_volume_is_removable (pThunarVolume))
 
1067
                                {
 
1068
                                        pThunarVolume = NULL;
 
1069
                                        continue;
 
1070
                                }
 
1071
                                
 
1072
                                g_signal_handlers_disconnect_matched (pThunarVolume, G_SIGNAL_MATCH_FUNC, 0, 0, NULL, _vfs_backend_volume_changed_callback, NULL);
 
1073
                                
 
1074
                                /// Debug.
 
1075
                                ThunarVfsPath *pMountPointVfsPath = thunar_vfs_volume_get_mount_point(pThunarVolume);
 
1076
                                gchar *ltmp_path = thunar_vfs_path_dup_uri(pMountPointVfsPath);
 
1077
                                cd_debug (" signal retire sur %s", ltmp_path);
 
1078
                                g_free (ltmp_path);
 
1079
                        }
 
1080
                }
 
1081
        }
 
1082
}
 
1083
 
 
1084
 
 
1085
gboolean vfs_backend_delete_file (const gchar *cURI)
 
1086
{
 
1087
        GError *erreur = NULL;
 
1088
        cd_message ("%s (%s)", __func__, cURI);
 
1089
 
 
1090
        ThunarVfsPath *pThunarPath = thunar_vfs_path_new(cURI, &erreur);
 
1091
        if (erreur != NULL)
 
1092
        {
 
1093
                cd_warning ("Attention : %s", erreur->message);
 
1094
                g_error_free (erreur);
 
1095
                return FALSE;
 
1096
        }
 
1097
 
 
1098
        ThunarVfsJob *pJob = thunar_vfs_unlink_file(pThunarPath, &erreur);
 
1099
        g_object_unref(pJob);
 
1100
 
 
1101
        thunar_vfs_path_unref(pThunarPath);
 
1102
 
 
1103
        return TRUE;
 
1104
}
 
1105
 
 
1106
gboolean vfs_backend_rename_file (const gchar *cOldURI, const gchar *cNewName)
 
1107
{
 
1108
        GError *erreur = NULL;
 
1109
        g_return_val_if_fail (cOldURI != NULL, FALSE);
 
1110
        cd_message ("%s (%s)", __func__, cOldURI);
 
1111
 
 
1112
        ThunarVfsPath *pThunarPath = thunar_vfs_path_new(cOldURI, &erreur);
 
1113
        if (erreur != NULL)
 
1114
        {
 
1115
                cd_warning ("Attention : %s", erreur->message);
 
1116
                g_error_free (erreur);
 
1117
                return FALSE;
 
1118
        }
 
1119
    ThunarVfsInfo *pThunarVfsInfo = thunar_vfs_info_new_for_path(pThunarPath, &erreur);
 
1120
        thunar_vfs_path_unref(pThunarPath);
 
1121
        if (erreur != NULL)
 
1122
        {
 
1123
                cd_warning ("Attention : %s", erreur->message);
 
1124
                g_error_free (erreur);
 
1125
                return FALSE;
 
1126
        }
 
1127
 
 
1128
        thunar_vfs_info_rename(pThunarVfsInfo, cNewName, &erreur );
 
1129
        thunar_vfs_info_unref(pThunarVfsInfo);
 
1130
        if (erreur != NULL)
 
1131
        {
 
1132
                cd_warning ("Attention : %s", erreur->message);
 
1133
                g_error_free (erreur);
 
1134
                return FALSE;
 
1135
        }
 
1136
 
 
1137
        return TRUE;
 
1138
}
 
1139
 
 
1140
gboolean vfs_backend_move_file (const gchar *cURI, const gchar *cDirectoryURI)
 
1141
{
 
1142
        /*
 
1143
         @see thunar_vfs_move_file
 
1144
        */
 
1145
        GError *erreur = NULL;
 
1146
        g_return_val_if_fail (cURI != NULL, FALSE);
 
1147
        cd_message ("%s (%s, %s)", __func__, cURI, cDirectoryURI);
 
1148
 
 
1149
        ThunarVfsPath *pThunarPathFrom = thunar_vfs_path_new(cURI, &erreur);
 
1150
        if (erreur != NULL)
 
1151
        {
 
1152
                cd_warning ("Attention : %s", erreur->message);
 
1153
                g_error_free (erreur);
 
1154
                return FALSE;
 
1155
        }
 
1156
 
 
1157
        gchar *cDestURI = g_strdup_printf("%s/%s", cDirectoryURI,  thunar_vfs_path_get_name(pThunarPathFrom));
 
1158
        ThunarVfsPath *pThunarPathTo = thunar_vfs_path_new(cDestURI, &erreur);
 
1159
        g_free(cDestURI);
 
1160
        if (erreur != NULL)
 
1161
        {
 
1162
                thunar_vfs_path_unref(pThunarPathFrom);
 
1163
                cd_warning ("Attention : %s", erreur->message);
 
1164
                g_error_free (erreur);
 
1165
                return FALSE;
 
1166
        }
 
1167
 
 
1168
        ThunarVfsJob *pJob = thunar_vfs_move_file(pThunarPathFrom, pThunarPathTo, &erreur);
 
1169
 
 
1170
        thunar_vfs_path_unref(pThunarPathFrom);
 
1171
        thunar_vfs_path_unref(pThunarPathTo);
 
1172
 
 
1173
        if (erreur != NULL)
 
1174
        {
 
1175
                cd_warning ("Attention : %s", erreur->message);
 
1176
                g_error_free (erreur);
 
1177
                return FALSE;
 
1178
        }
 
1179
 
 
1180
        g_object_unref(pJob);
 
1181
        return TRUE;
 
1182
}
 
1183
 
 
1184
void vfs_backend_get_file_properties (const gchar *cURI, guint64 *iSize, time_t *iLastModificationTime, gchar **cMimeType, int *iUID, int *iGID, int *iPermissionsMask)
 
1185
{
 
1186
        GError *erreur = NULL;
 
1187
        g_return_if_fail (cURI != NULL);
 
1188
        cd_message ("%s (%s)", __func__, cURI);
 
1189
 
 
1190
        ThunarVfsPath *pThunarPath = thunar_vfs_path_new(cURI, &erreur);
 
1191
        if (erreur != NULL)
 
1192
        {
 
1193
                cd_warning ("Attention : %s", erreur->message);
 
1194
                g_error_free (erreur);
 
1195
                return;
 
1196
        }
 
1197
    ThunarVfsInfo *pThunarVfsInfo = thunar_vfs_info_new_for_path(pThunarPath, &erreur);
 
1198
        thunar_vfs_path_unref(pThunarPath);
 
1199
        if (erreur != NULL)
 
1200
        {
 
1201
                cd_warning ("Attention : %s", erreur->message);
 
1202
                g_error_free (erreur);
 
1203
                return;
 
1204
        }
 
1205
 
 
1206
        *iSize = pThunarVfsInfo->size;
 
1207
        *iLastModificationTime = pThunarVfsInfo->mtime;
 
1208
        *cMimeType = g_strdup (thunar_vfs_mime_info_get_name(pThunarVfsInfo->mime_info));
 
1209
        *iUID = pThunarVfsInfo->uid;
 
1210
        *iGID = pThunarVfsInfo->gid;
 
1211
        *iPermissionsMask = pThunarVfsInfo->mode;
 
1212
 
 
1213
        thunar_vfs_info_unref(pThunarVfsInfo);
 
1214
}
 
1215
 
 
1216
 
 
1217
gchar *vfs_backend_get_trash_path (const gchar *cNearURI, gchar **cFileInfoPath)
 
1218
{
 
1219
        GError *erreur = NULL;
 
1220
        cd_message ("%s (%s)", __func__, cNearURI);
 
1221
 
 
1222
        ThunarVfsPath *pThunarPath = thunar_vfs_path_new("trash:/", &erreur);
 
1223
        if (erreur != NULL)
 
1224
        {
 
1225
                cd_warning ("Attention : %s", erreur->message);
 
1226
                g_error_free (erreur);
 
1227
                return NULL;
 
1228
        }
 
1229
        thunar_vfs_path_unref(pThunarPath);
 
1230
        
 
1231
        /*
 
1232
        * If we haven't returned NULL yet, it means that there exist a valid trash.
 
1233
        * We know that Thunar follows the XDG recommendations.
 
1234
        * So the trash is in ~/.local/share/Trash
 
1235
        */
 
1236
        
 
1237
        gchar *trashPath = NULL;
 
1238
        char *home = getenv("HOME");
 
1239
        if( home )
 
1240
        {
 
1241
                trashPath = g_strdup_printf("%s/%s", home,  ".local/share/Trash/files");
 
1242
                if (cFileInfoPath != NULL)
 
1243
                        *cFileInfoPath = g_strdup_printf ("%s/.local/share/Trash/info", home);
 
1244
        }
 
1245
        
 
1246
        return trashPath;
 
1247
}
 
1248
 
 
1249
gchar *vfs_backend_get_desktop_path (void)
 
1250
{
 
1251
        GError *erreur = NULL;
 
1252
        cd_message ("");
 
1253
 
 
1254
        ThunarVfsPath *pThunarDesktopPath = thunar_vfs_path_new("desktop:/", &erreur);
 
1255
        if (erreur != NULL)
 
1256
        {
 
1257
                cd_warning ("Attention : %s", erreur->message);
 
1258
                g_error_free (erreur);
 
1259
                return NULL;
 
1260
        }
 
1261
        thunar_vfs_path_unref(pThunarDesktopPath);
 
1262
 
 
1263
    /*
 
1264
     * If we haven't returned NULL yet, it means that there exist a valid desktop.
 
1265
     * We know that Thunar follows the XDG recommendations.
 
1266
     * So the trash is in ~/Desktop
 
1267
     */
 
1268
 
 
1269
    gchar *desktopPath = NULL;
 
1270
    char *home = getenv("HOME");
 
1271
    if( home )
 
1272
    {
 
1273
            desktopPath = g_strdup_printf("%s/%s", home,  "Desktop");
 
1274
    }
 
1275
 
 
1276
        return desktopPath;
 
1277
}