Debian Codegraph @

Scooby-Do/src/applet-backend-files.c

raw
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 #include <stdlib.h>
21 #include <string.h>
22 #include <time.h>
23
24 #include <glib/gstdio.h>
25
26 #include "applet-struct.h"
27 #include "applet-notifications.h"
28 #include "applet-session.h"
29 #include "applet-listing.h"
30 #include "applet-search.h"
31 #include "applet-backend-files.h"
32
33 // sub-listing
34 static GList *_cd_do_list_file_sub_entries (CDEntry *pEntry, int *iNbEntries);
35 // fill entry
36 static gboolean _cd_do_fill_main_entry (CDEntry *pEntry);
37 static gboolean _cd_do_fill_file_entry (CDEntry *pEntry);
38 // actions
39 static void _cd_do_launch_file (CDEntry *pEntry);
40 static void _cd_do_show_file_location (CDEntry *pEntry);
41 static void _cd_do_open_terminal_here (CDEntry *pEntry);
42 static void _cd_do_zip_file (CDEntry *pEntry);
43 static void _cd_do_zip_folder (CDEntry *pEntry);
44 static void _cd_do_mail_file (CDEntry *pEntry);
45 static void _cd_do_move_file (CDEntry *pEntry);
46 static void _cd_do_copy_url (CDEntry *pEntry);
47
48
49 //////////
50 // INIT //
51 //////////
52
53 static gboolean init (void)
54 {
55 gchar *cResult = cairo_dock_launch_command_sync ("which locate");
56
57 gboolean bAvailable = (cResult != NULL && *cResult != '\0');
58 g_free (cResult);
59 cd_debug ("locate available : %d", bAvailable);
60
61 if (bAvailable)
62 {
63 gchar *cDirPath = g_strdup_printf ("%s/ScoobyDo", g_cCairoDockDataDir);
64 if (! g_file_test (cDirPath, G_FILE_TEST_IS_DIR))
65 {
66 if (g_mkdir (cDirPath, 7*8*8+7*8+5) != 0)
67 {
68 cd_warning ("couldn't create directory %s", cDirPath);
69 g_free (cDirPath);
70 return FALSE;
71 }
72 }
73
74 gchar *cDataBase = g_strdup_printf ("%s/ScoobyDo.db", cDirPath);
75 gchar *cLastUpdateFile = g_strdup_printf ("%s/.last-update", cDirPath);
76 gboolean bNeedsUpdate = FALSE;
77
78 if (! g_file_test (cDataBase, G_FILE_TEST_EXISTS))
79 {
80 bNeedsUpdate = TRUE;
81 }
82 else
83 {
84 if (! g_file_test (cLastUpdateFile, G_FILE_TEST_EXISTS))
85 {
86 bNeedsUpdate = TRUE;
87 }
88 else
89 {
90 gsize length = 0;
91 gchar *cContent = NULL;
92 g_file_get_contents (cLastUpdateFile,
93 &cContent,
94 &length,
95 NULL);
96 if (cContent == NULL || *cContent == '\0')
97 {
98 bNeedsUpdate = TRUE;
99 }
100 else
101 {
102 time_t iLastUpdateTime = atoll (cContent);
103 time_t iCurrentTime = (time_t) time (NULL);
104 if (iCurrentTime - iLastUpdateTime > 86400)
105 {
106 bNeedsUpdate = TRUE;
107 }
108 }
109 g_free (cContent);
110 }
111 }
112
113 if (bNeedsUpdate)
114 {
115 cairo_dock_launch_command (MY_APPLET_SHARE_DATA_DIR"/updatedb.sh");
116 gchar *cDate = g_strdup_printf ("%ld", time (NULL));
117 g_file_set_contents (cLastUpdateFile,
118 cDate,
119 -1,
120 NULL);
121 g_free (cDate);
122 }
123
124 g_free (cDataBase);
125 g_free (cLastUpdateFile);
126 g_free (cDirPath);
127 }
128
129 return bAvailable;
130 }
131
132
133
134
135
136 /////////////////
137 // SUB-LISTING //
138 /////////////////
139
140 #define NB_ACTIONS_ON_FOLDER 4
141 static GList *_list_folder (const gchar *cPath, gboolean bNoHiddenFile, int *iNbEntries)
142 {
143 cd_debug ("%s (%s)", __func__, cPath);
144 // on ouvre le repertoire.
145 GError *erreur = NULL;
146 GDir *dir = g_dir_open (cPath, 0, &erreur);
147 if (erreur != NULL)
148 {
149 cd_warning (erreur->message);
150 g_error_free (erreur);
151 *iNbEntries = 0;
152 return NULL;
153 }
154
155 // on ajoute les entrees d'actions sur le repertoire.
156 GList *pEntries = NULL;
157 CDEntry *pEntry;
158
159 pEntry = g_new0 (CDEntry, 1);
160 pEntry->cPath = g_strdup (cPath);
161 pEntry->cName = g_strdup (D_("Open a terminal here"));
162 pEntry->cIconName = g_strdup ("terminal");
163 pEntry->fill = cd_do_fill_default_entry;
164 pEntry->execute = _cd_do_open_terminal_here;
165 pEntries = g_list_prepend (pEntries, pEntry);
166
167 pEntry = g_new0 (CDEntry, 1);
168 pEntry->cPath = g_strdup (cPath);
169 pEntry->cName = g_strdup (D_("Zip folder"));
170 pEntry->cIconName = g_strdup ("zip");
171 pEntry->fill = cd_do_fill_default_entry;
172 pEntry->execute = _cd_do_zip_folder;
173 pEntries = g_list_prepend (pEntries, pEntry);
174
175 pEntry = g_new0 (CDEntry, 1);
176 pEntry->cPath = g_strdup (cPath);
177 pEntry->cName = g_strdup (D_("Move to"));
178 pEntry->cIconName = g_strdup (GLDI_ICON_NAME_JUMP_TO);
179 pEntry->fill = cd_do_fill_default_entry;
180 pEntry->execute = _cd_do_move_file;
181 pEntries = g_list_prepend (pEntries, pEntry);
182
183 pEntry = g_new0 (CDEntry, 1);
184 pEntry->cPath = g_strdup (cPath);
185 pEntry->cName = g_strdup (D_("Copy URL"));
186 pEntry->cIconName = g_strdup (GLDI_ICON_NAME_COPY);
187 pEntry->fill = cd_do_fill_default_entry;
188 pEntry->execute = _cd_do_copy_url;
189 pEntries = g_list_prepend (pEntries, pEntry);
190
191 // on ajoute une entree par fichier du repertoire.
192 int iNbFiles = 0;
193 const gchar *cFileName;
194 do
195 {
196 cFileName = g_dir_read_name (dir);
197 if (cFileName == NULL)
198 break ;
199 if (bNoHiddenFile && *cFileName == '.')
200 continue;
201
202 pEntry = g_new0 (CDEntry, 1);
203 pEntry->cName = g_strdup (cFileName);
204 pEntry->cPath = g_strdup_printf ("%s/%s", cPath, cFileName);
205 pEntry->fill = _cd_do_fill_file_entry;
206 pEntry->execute = _cd_do_launch_file;
207 pEntry->list = _cd_do_list_file_sub_entries;
208 pEntries = g_list_prepend (pEntries, pEntry);
209 iNbFiles ++;
210 }
211 while (1);
212 g_dir_close (dir);
213
214 *iNbEntries = iNbFiles + NB_ACTIONS_ON_FOLDER;
215 return pEntries;
216 }
217
218 #define NB_ACTIONS_ON_FILE 5
219 static GList *_list_actions_on_file (const gchar *cPath, int *iNbEntries)
220 {
221 cd_debug ("%s ()", __func__);
222 GList *pEntries = NULL;
223 CDEntry *pEntry;
224
225 pEntry = g_new0 (CDEntry, 1);
226 pEntry->cPath = g_strdup (cPath);
227 pEntry->cName = g_strdup (D_("Open location"));
228 pEntry->cIconName = g_strdup (GLDI_ICON_NAME_DIRECTORY);
229 pEntry->fill = cd_do_fill_default_entry;
230 pEntry->execute = _cd_do_show_file_location;
231 pEntries = g_list_prepend (pEntries, pEntry);
232
233 pEntry = g_new0 (CDEntry, 1);
234 pEntry->cPath = g_strdup (cPath);
235 pEntry->cName = g_strdup (D_("Zip file"));
236 pEntry->cIconName = g_strdup ("zip");
237 pEntry->fill = cd_do_fill_default_entry;
238 pEntry->execute = _cd_do_zip_file;
239 pEntries = g_list_prepend (pEntries, pEntry);
240
241 pEntry = g_new0 (CDEntry, 1);
242 pEntry->cPath = g_strdup (cPath);
243 pEntry->cName = g_strdup (D_("Mail file"));
244 pEntry->cIconName = g_strdup ("thunderbird"); /// utiliser le client mail par defaut...
245 pEntry->fill = cd_do_fill_default_entry;
246 pEntry->execute = _cd_do_mail_file;
247 pEntries = g_list_prepend (pEntries, pEntry);
248
249 pEntry = g_new0 (CDEntry, 1);
250 pEntry->cPath = g_strdup (cPath);
251 pEntry->cName = g_strdup (D_("Move to"));
252 pEntry->cIconName = g_strdup (GLDI_ICON_NAME_JUMP_TO);
253 pEntry->fill = cd_do_fill_default_entry;
254 pEntry->execute = _cd_do_move_file;
255 pEntries = g_list_prepend (pEntries, pEntry);
256
257 pEntry = g_new0 (CDEntry, 1);
258 pEntry->cPath = g_strdup (cPath);
259 pEntry->cName = g_strdup (D_("Copy URL"));
260 pEntry->cIconName = g_strdup (GLDI_ICON_NAME_COPY);
261 pEntry->fill = cd_do_fill_default_entry;
262 pEntry->execute = _cd_do_copy_url;
263 pEntries = g_list_prepend (pEntries, pEntry);
264
265 *iNbEntries = NB_ACTIONS_ON_FILE;
266 return pEntries;
267 }
268
269 static GList *_cd_do_list_file_sub_entries (CDEntry *pEntry, int *iNbEntries)
270 {
271 cd_debug ("%s (%s)", __func__, pEntry->cPath);
272 if (pEntry->cPath == NULL) // on est deja en bout de chaine.
273 return NULL;
274 if (g_file_test (pEntry->cPath, G_FILE_TEST_IS_DIR)) // on liste les fichiers du repertoire et les actions sur le repertoire.
275 {
276 return _list_folder (pEntry->cPath, TRUE, iNbEntries); // TRUE <=> no hidden files.
277 }
278 else // on liste les actions sur le fichier.
279 {
280 return _list_actions_on_file (pEntry->cPath, iNbEntries);
281 }
282 }
283
284
285
286 ////////////////
287 // FILL ENTRY //
288 ////////////////
289
290 static gboolean _cd_do_fill_main_entry (CDEntry *pEntry)
291 {
292 if (pEntry->cIconName && pEntry->pIconSurface == NULL)
293 {
294 gchar *cImagePath = g_strconcat (MY_APPLET_SHARE_DATA_DIR, "/", pEntry->cIconName, NULL);
295 pEntry->pIconSurface = cairo_dock_create_surface_from_icon (cImagePath,
296 myDialogsParam.dialogTextDescription.iSize + 2,
297 myDialogsParam.dialogTextDescription.iSize + 2);
298 g_free (cImagePath);
299 return TRUE;
300 }
301 return FALSE;
302 }
303
304 static gboolean _cd_do_fill_file_entry (CDEntry *pEntry)
305 {
306 gchar *cName = NULL, *cURI = NULL, *cIconName = NULL;
307 gboolean bIsDirectory;
308 int iVolumeID;
309 double fOrder;
310 cairo_dock_fm_get_file_info (pEntry->cPath, &cName, &cURI, &cIconName, &bIsDirectory, &iVolumeID, &fOrder, 0);
311 g_free (cName);
312 g_free (cURI);
313 if (cIconName != NULL && pEntry->pIconSurface == NULL)
314 {
315 pEntry->pIconSurface = cairo_dock_create_surface_from_icon (cIconName,
316 myDialogsParam.dialogTextDescription.iSize,
317 myDialogsParam.dialogTextDescription.iSize);
318 g_free (cIconName);
319 return TRUE;
320 }
321 return FALSE;
322 }
323
324
325 /////////////
326 // ACTIONS //
327 /////////////
328
329 static void _cd_do_launch_file (CDEntry *pEntry)
330 {
331 cd_debug ("%s (%s)", __func__, pEntry->cPath);
332 cairo_dock_fm_launch_uri (pEntry->cPath);
333 }
334
335 static void _cd_do_show_file_location (CDEntry *pEntry)
336 {
337 cd_debug ("%s (%s)", __func__, pEntry->cPath);
338 gchar *cPathUp = g_path_get_dirname (pEntry->cPath);
339 g_return_if_fail (cPathUp != NULL);
340 cairo_dock_fm_launch_uri (cPathUp);
341 g_free (cPathUp);
342 }
343
344 static void _cd_do_open_terminal_here (CDEntry *pEntry)
345 {
346 cd_debug ("%s (%s)", __func__, pEntry->cPath);
347 gchar *cCommand = NULL;
348 if (g_iDesktopEnv == CAIRO_DOCK_GNOME)
349 cCommand = g_strdup_printf ("gnome-terminal --working-directory=\"%s\"", pEntry->cPath);
350 else if (g_iDesktopEnv == CAIRO_DOCK_KDE)
351 cCommand = g_strdup_printf ("konsole --workdir \"%s\"", pEntry->cPath);
352
353 if (cCommand != NULL)
354 {
355 cairo_dock_launch_command (cCommand);
356 g_free (cCommand);
357 }
358 }
359
360 static void _cd_do_zip_folder (CDEntry *pEntry)
361 {
362 cd_debug ("%s (%s)", __func__, pEntry->cPath);
363 gchar *cCommand = g_strdup_printf ("tar cfz '%s.tar.gz' '%s'", pEntry->cPath, pEntry->cPath);
364 cairo_dock_launch_command (cCommand);
365 g_free (cCommand);
366 }
367
368 static void _cd_do_zip_file (CDEntry *pEntry)
369 {
370 cd_debug ("%s (%s)", __func__, pEntry->cPath);
371 gchar *cCommand = g_strdup_printf ("zip '%s.zip' '%s'", pEntry->cPath, pEntry->cPath);
372 cairo_dock_launch_command (cCommand);
373 g_free (cCommand);
374 }
375
376 static void _cd_do_mail_file (CDEntry *pEntry)
377 {
378 cd_debug ("%s (%s)", __func__, pEntry->cPath);
379 gchar *cURI = g_filename_to_uri (pEntry->cPath, NULL, NULL);
380 gchar *cCommand = g_strdup_printf ("thunderbird -compose \"attachment=%s\"", cURI); /// prendre aussi en compte les autres clients mail, et utiliser celui par defaut...
381 cairo_dock_launch_command (cCommand);
382 g_free (cCommand);
383 g_free (cURI);
384 }
385
386 static void _cd_do_move_file (CDEntry *pEntry)
387 {
388 cd_debug ("%s (%s)", __func__, pEntry->cPath);
389 GtkWidget* pFileChooserDialog = gtk_file_chooser_dialog_new (
390 D_("Pick up a directory"),
391 GTK_WINDOW (g_pMainDock->container.pWidget),
392 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
393 _("Ok"),
394 GTK_RESPONSE_OK,
395 _("Cancel"),
396 GTK_RESPONSE_CANCEL,
397 NULL);
398 gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (pFileChooserDialog), FALSE);
399
400 gtk_widget_show (pFileChooserDialog);
401 int answer = gtk_dialog_run (GTK_DIALOG (pFileChooserDialog));
402 if (answer == GTK_RESPONSE_OK)
403 {
404 gchar *cDirPath = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (pFileChooserDialog));
405
406 gchar *cFileName = g_path_get_basename (pEntry->cPath);
407 gchar *cNewFilePath = g_strdup_printf ("%s/%s", cDirPath, cFileName);
408 g_return_if_fail (! g_file_test (cNewFilePath, G_FILE_TEST_EXISTS));
409 g_free (cFileName);
410 g_free (cNewFilePath);
411
412 gchar *cCommand = g_strdup_printf ("mv '%s' '%s'", pEntry->cPath, cDirPath);
413 cairo_dock_launch_command (cCommand);
414 g_free (cCommand);
415 }
416 gtk_widget_destroy (pFileChooserDialog);
417 }
418
419 static void _cd_do_copy_url (CDEntry *pEntry)
420 {
421 cd_debug ("%s (%s)", __func__, pEntry->cPath);
422 GtkClipboard *pClipBoard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
423 gtk_clipboard_set_text (pClipBoard, pEntry->cPath, -1);
424 }
425
426
427 ////////////
428 // SEARCH //
429 ////////////
430
431 static gchar *_locate_files (const char *text, int iFilter, gint iLimit)
432 {
433 GString *sCommand = g_string_new ("locate");
434 g_string_append_printf (sCommand, " -d '%s/ScoobyDo/ScoobyDo.db'", g_cCairoDockDataDir);
435 if (iLimit > 0)
436 g_string_append_printf (sCommand, " --limit=%d", iLimit);
437 if (! (iFilter & DO_MATCH_CASE))
438 g_string_append (sCommand, " -i");
439 if (*text != '/')
440 g_string_append (sCommand, " -b");
441
442 if (iFilter == DO_FILTER_NONE || iFilter == DO_MATCH_CASE)
443 {
444 g_string_append_printf (sCommand, " \"%s\"", text);
445 }
446 else
447 {
448 if (iFilter & DO_TYPE_MUSIC)
449 {
450 g_string_append_printf (sCommand, " \"*%s*.mp3\" \"*%s*.ogg\" \"*%s*.wav\"", text, text, text);
451 }
452 if (iFilter & DO_TYPE_IMAGE)
453 {
454 g_string_append_printf (sCommand, " \"*%s*.jpg\" \"*%s*.jpeg\" \"*%s*.png\"", text, text, text);
455 }
456 if (iFilter & DO_TYPE_VIDEO)
457 {
458 g_string_append_printf (sCommand, " \"*%s*.avi\" \"*%s*.mkv\" \"*%s*.ogv\" \"*%s*.wmv\" \"*%s*.mov\"", text, text, text, text, text);
459 }
460 if (iFilter & DO_TYPE_TEXT)
461 {
462 g_string_append_printf (sCommand, " \"*%s*.txt\" \"*%s*.odt\" \"*%s*.doc\"", text, text, text);
463 }
464 if (iFilter & DO_TYPE_HTML)
465 {
466 g_string_append_printf (sCommand, " \"*%s*.html\" \"*%s*.htm\"", text, text);
467 }
468 if (iFilter & DO_TYPE_SOURCE)
469 {
470 g_string_append_printf (sCommand, " \"*%s*.[ch]\" \"*%s*.cpp\"", text, text);
471 }
472 }
473
474 cd_debug (">>> %s", sCommand->str);
475 gchar *cResult = cairo_dock_launch_command_sync (sCommand->str);
476 if (cResult == NULL || *cResult == '\0')
477 {
478 g_free (cResult);
479 return NULL;
480 }
481
482 return cResult;
483 }
484 static GList * _build_entries (gchar *cResult, int *iNbEntries)
485 {
486 gchar **pMatchingFiles = g_strsplit (cResult, "\n", 0);
487
488 GList *pEntries = NULL;
489 CDEntry *pEntry;
490 int i;
491 for (i = 0; pMatchingFiles[i] != NULL; i ++)
492 {
493 pEntry = g_new0 (CDEntry, 1);
494 pEntry->cPath = pMatchingFiles[i];
495 pEntry->cName = g_path_get_basename (pEntry->cPath);
496 pEntry->fill = _cd_do_fill_file_entry;
497 pEntry->execute = _cd_do_launch_file;
498 pEntry->list = _cd_do_list_file_sub_entries;
499 pEntries = g_list_prepend (pEntries, pEntry);
500 }
501 g_free (pMatchingFiles); // ses elements sont dans les entrees.
502
503 cd_debug ("%d entries built", i);
504 *iNbEntries = i;
505 return pEntries;
506 }
507 static GList* search (const gchar *cText, int iFilter, gboolean bSearchAll, int *iNbEntries)
508 {
509 cd_debug ("%s (%s)", __func__, cText);
510 gchar *cResult = _locate_files (cText, iFilter, (bSearchAll ? 50 : 3));
511
512 if (cResult == NULL)
513 {
514 *iNbEntries = 0;
515 return NULL;
516 }
517 GList *pEntries = _build_entries (cResult, iNbEntries);
518 g_free (cResult);
519
520 if (!bSearchAll && pEntries != NULL)
521 {
522 CDEntry *pEntry = g_new0 (CDEntry, 1);
523 pEntry->cPath = g_strdup ("Files");
524 pEntry->cName = g_strdup (D_("Files"));
525 pEntry->cIconName = g_strdup ("files.png");
526 pEntry->bMainEntry = TRUE;
527 pEntry->fill = _cd_do_fill_main_entry;
528 pEntry->list = cd_do_list_main_sub_entry;
529 pEntries = g_list_prepend (pEntries, pEntry);
530 *iNbEntries = (*iNbEntries + 1);
531 }
532
533 return pEntries;
534 }
535
536
537 //////////////
538 // REGISTER //
539 //////////////
540
541 void cd_do_register_files_backend (void)
542 {
543 CDBackend *pBackend = g_new0 (CDBackend, 1);
544 pBackend->cName = "Files";
545 pBackend->bIsThreaded = TRUE;
546 pBackend->init =(CDBackendInitFunc) init;
547 pBackend->search = (CDBackendSearchFunc) search;
548 myData.pBackends = g_list_prepend (myData.pBackends, pBackend);
549 }