| 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-recent.h" |
| 32 |
|
| 33 |
static GtkRecentManager *s_pRecentMgr = NULL; |
| 34 |
|
| 35 |
////////// |
| 36 |
// INIT // |
| 37 |
////////// |
| 38 |
|
| 39 |
static gboolean init (void) |
| 40 |
{ |
| 41 |
s_pRecentMgr = gtk_recent_manager_get_default (); |
| 42 |
return TRUE; |
| 43 |
} |
| 44 |
|
| 45 |
static void stop (void) |
| 46 |
{ |
| 47 |
s_pRecentMgr = NULL; // pas de "unref" a faire. |
| 48 |
} |
| 49 |
|
| 50 |
//////////////// |
| 51 |
// FILL ENTRY // |
| 52 |
//////////////// |
| 53 |
|
| 54 |
static gboolean _cd_do_fill_recent_entry (CDEntry *pEntry) |
| 55 |
{ |
| 56 |
if (pEntry->pIconSurface == NULL) |
| 57 |
{ |
| 58 |
GtkRecentInfo *pInfo = gtk_recent_manager_lookup_item (s_pRecentMgr, |
| 59 |
pEntry->cPath, |
| 60 |
NULL); |
| 61 |
if (pInfo != NULL) |
| 62 |
{ |
| 63 |
GdkPixbuf *pixbuf = gtk_recent_info_get_icon (pInfo, myDialogsParam.dialogTextDescription.iSize + 2); |
| 64 |
if (pixbuf != NULL) |
| 65 |
{ |
| 66 |
double fImageWidth, fImageHeight; |
| 67 |
pEntry->pIconSurface = cairo_dock_create_surface_from_pixbuf (pixbuf, |
| 68 |
1., |
| 69 |
myDialogsParam.dialogTextDescription.iSize, // width |
| 70 |
myDialogsParam.dialogTextDescription.iSize, // height |
| 71 |
0, // modifier |
| 72 |
&fImageWidth, &fImageHeight, |
| 73 |
NULL, NULL); |
| 74 |
g_object_unref (pixbuf); |
| 75 |
return TRUE; |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
return FALSE; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
///////////// |
| 84 |
// ACTIONS // |
| 85 |
///////////// |
| 86 |
|
| 87 |
static void _cd_do_launch_file (CDEntry *pEntry) |
| 88 |
{ |
| 89 |
cd_debug ("%s (%s)", __func__, pEntry->cPath); |
| 90 |
cairo_dock_fm_launch_uri (pEntry->cPath); |
| 91 |
} |
| 92 |
|
| 93 |
static void _cd_do_launch_file_with_given_app (CDEntry *pEntry) |
| 94 |
{ |
| 95 |
cd_debug ("%s (%s)", __func__, pEntry->cPath); |
| 96 |
cairo_dock_launch_command (pEntry->cPath); |
| 97 |
} |
| 98 |
|
| 99 |
static void _cd_do_show_file_location (CDEntry *pEntry) |
| 100 |
{ |
| 101 |
cd_debug ("%s (%s)", __func__, pEntry->cPath); |
| 102 |
gchar *cPathUp = g_path_get_dirname (pEntry->cPath); |
| 103 |
g_return_if_fail (cPathUp != NULL); |
| 104 |
cairo_dock_fm_launch_uri (cPathUp); |
| 105 |
g_free (cPathUp); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
///////////////// |
| 110 |
// SUB-LISTING // |
| 111 |
///////////////// |
| 112 |
|
| 113 |
static GList *_cd_do_list_recent_sub_entries (CDEntry *pEntry, int *iNbEntries) |
| 114 |
{ |
| 115 |
cd_debug ("%s (%s)", __func__, pEntry->cPath); |
| 116 |
CDEntry *pSubEntry; |
| 117 |
GList *pEntries = NULL; |
| 118 |
int i = 0; |
| 119 |
|
| 120 |
GtkRecentInfo *pInfo = gtk_recent_manager_lookup_item (s_pRecentMgr, |
| 121 |
pEntry->cPath, |
| 122 |
NULL); |
| 123 |
if (pInfo != NULL) |
| 124 |
{ |
| 125 |
gchar **pApps = gtk_recent_info_get_applications (pInfo, NULL); |
| 126 |
if (pApps != NULL) |
| 127 |
{ |
| 128 |
int j; |
| 129 |
for (j = 0; pApps[j] != NULL; j ++) |
| 130 |
{ |
| 131 |
pSubEntry = g_new0 (CDEntry, 1); |
| 132 |
pSubEntry->cPath = g_strdup_printf ("%s \"%s\"", pApps[j], pEntry->cPath); |
| 133 |
pSubEntry->cName = g_strdup_printf ("Open %s with %s", pEntry->cName, pApps[j]); |
| 134 |
pSubEntry->cIconName = g_strdup (pApps[j]); |
| 135 |
pSubEntry->execute = _cd_do_launch_file_with_given_app; |
| 136 |
pSubEntry->fill = cd_do_fill_default_entry; |
| 137 |
pEntries = g_list_prepend (pEntries, pSubEntry); |
| 138 |
i ++; |
| 139 |
} |
| 140 |
g_strfreev (pApps); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
pSubEntry = g_new0 (CDEntry, 1); |
| 145 |
pSubEntry->cPath = g_strdup (pEntry->cPath); |
| 146 |
pSubEntry->cName = g_strdup (D_("Open location")); |
| 147 |
pSubEntry->cIconName = g_strdup (GLDI_ICON_NAME_DIRECTORY); |
| 148 |
pSubEntry->execute = _cd_do_show_file_location; |
| 149 |
pSubEntry->fill = cd_do_fill_default_entry; |
| 150 |
pEntries = g_list_prepend (pEntries, pSubEntry); |
| 151 |
i ++; |
| 152 |
|
| 153 |
*iNbEntries = i; |
| 154 |
return pEntries; |
| 155 |
} |
| 156 |
|
| 157 |
//////////// |
| 158 |
// SEARCH // |
| 159 |
//////////// |
| 160 |
|
| 161 |
static GList* search (const gchar *cText, int iFilter, gboolean bSearchAll, int *iNbEntries) |
| 162 |
{ |
| 163 |
cd_debug ("%s (%s)", __func__, cText); |
| 164 |
|
| 165 |
GList *pEntries = NULL; |
| 166 |
CDEntry *pEntry; |
| 167 |
|
| 168 |
GList * pRecentFiles = gtk_recent_manager_get_items (s_pRecentMgr); |
| 169 |
if (pRecentFiles == NULL) |
| 170 |
{ |
| 171 |
*iNbEntries = 0; |
| 172 |
return NULL; |
| 173 |
} |
| 174 |
|
| 175 |
GtkRecentInfo *pInfo; |
| 176 |
const gchar *cName, *cUri; |
| 177 |
gchar *cLowerCaseName; |
| 178 |
int i = 0, iNbMax = (bSearchAll ? 50 : 3); |
| 179 |
GList *rf; |
| 180 |
for (rf = pRecentFiles, i = 0; rf != NULL && i < iNbMax; rf = rf->next) |
| 181 |
{ |
| 182 |
pInfo = rf->data; |
| 183 |
|
| 184 |
cName = gtk_recent_info_get_display_name (pInfo); |
| 185 |
cUri = gtk_recent_info_get_uri (pInfo); |
| 186 |
cLowerCaseName = g_ascii_strdown (cName, -1); |
| 187 |
if (cUri != NULL && cLowerCaseName != NULL && g_strstr_len (cLowerCaseName, -1, cText) != NULL) |
| 188 |
{ |
| 189 |
pEntry = g_new0 (CDEntry, 1); |
| 190 |
pEntry->cPath = g_strdup (cUri); |
| 191 |
pEntry->cName = g_strdup (cName); |
| 192 |
pEntry->cLowerCaseName = cLowerCaseName; |
| 193 |
pEntry->cIconName = NULL; |
| 194 |
pEntry->execute = _cd_do_launch_file; |
| 195 |
pEntry->fill = _cd_do_fill_recent_entry; |
| 196 |
pEntry->list = _cd_do_list_recent_sub_entries; |
| 197 |
pEntries = g_list_prepend (pEntries, pEntry); |
| 198 |
i ++; |
| 199 |
} |
| 200 |
else |
| 201 |
g_free (cLowerCaseName); |
| 202 |
|
| 203 |
gtk_recent_info_unref (pInfo); |
| 204 |
} |
| 205 |
g_list_free (pRecentFiles); |
| 206 |
|
| 207 |
pEntries = g_list_reverse (pEntries); // les plus recents en premier. |
| 208 |
|
| 209 |
if (! bSearchAll && i != 0) |
| 210 |
{ |
| 211 |
pEntry = g_new0 (CDEntry, 1); |
| 212 |
pEntry->cPath = NULL; |
| 213 |
pEntry->cName = g_strdup (D_("Recent files")); |
| 214 |
pEntry->cIconName = g_strdup (MY_APPLET_SHARE_DATA_DIR"/recent.png"); |
| 215 |
pEntry->bMainEntry = TRUE; |
| 216 |
pEntry->execute = NULL; |
| 217 |
pEntry->fill = cd_do_fill_default_entry; |
| 218 |
pEntry->list = cd_do_list_main_sub_entry; |
| 219 |
pEntries = g_list_prepend (pEntries, pEntry); |
| 220 |
i ++; |
| 221 |
} |
| 222 |
|
| 223 |
*iNbEntries = i; |
| 224 |
return pEntries; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
////////////// |
| 229 |
// REGISTER // |
| 230 |
////////////// |
| 231 |
|
| 232 |
void cd_do_register_recent_backend (void) |
| 233 |
{ |
| 234 |
CDBackend *pBackend = g_new0 (CDBackend, 1); |
| 235 |
pBackend->cName = "Recent"; |
| 236 |
pBackend->bIsThreaded = FALSE; |
| 237 |
pBackend->init = (CDBackendInitFunc) init; |
| 238 |
pBackend->stop = (CDBackendStopFunc) stop; |
| 239 |
pBackend->search = (CDBackendSearchFunc) search; |
| 240 |
myData.pBackends = g_list_prepend (myData.pBackends, pBackend); |
| 241 |
} |