| 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 |
|
| 23 |
#define __USE_BSD 1 |
| 24 |
#include <string.h> |
| 25 |
#include <dirent.h> |
| 26 |
#include <errno.h> |
| 27 |
#include <sys/types.h> |
| 28 |
#include <unistd.h> |
| 29 |
#include <sys/stat.h> |
| 30 |
|
| 31 |
#include "applet-struct.h" |
| 32 |
#include "applet-appli-finder.h" |
| 33 |
|
| 34 |
static void _browse_dir (const gchar *cDirPath); |
| 35 |
|
| 36 |
|
| 37 |
void cd_do_reset_applications_list (void) |
| 38 |
{ |
| 39 |
g_list_foreach (myData.pApplications, (GFunc) gldi_object_unref, NULL); |
| 40 |
g_list_free (myData.pApplications); |
| 41 |
myData.pApplications = NULL; |
| 42 |
|
| 43 |
GList *m; |
| 44 |
for (m = myData.pMonitorList; m != NULL; m = m->next) |
| 45 |
{ |
| 46 |
cairo_dock_fm_remove_monitor_full (m->data, TRUE, NULL); |
| 47 |
g_free (m->data); |
| 48 |
} |
| 49 |
g_list_free (myData.pMonitorList); |
| 50 |
myData.pMonitorList = NULL; |
| 51 |
|
| 52 |
myData.pCurrentApplicationToLoad = NULL; |
| 53 |
if (myData.iSidLoadExternAppliIdle != 0) |
| 54 |
{ |
| 55 |
g_source_remove (myData.iSidLoadExternAppliIdle); |
| 56 |
myData.iSidLoadExternAppliIdle = 0; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
static int _compare_appli (Icon *pIcon1, Icon *pIcon2) |
| 61 |
{ |
| 62 |
if (pIcon1->cCommand == NULL) |
| 63 |
return -1; |
| 64 |
if (pIcon2->cCommand == NULL) |
| 65 |
return 1; |
| 66 |
return strcmp (pIcon1->cCommand, pIcon2->cCommand); |
| 67 |
} |
| 68 |
static void _cd_do_on_file_event (CairoDockFMEventType iEventType, const gchar *cURI, gpointer data) |
| 69 |
{ |
| 70 |
cd_debug ("la liste des applis a change dans %s!", cURI); |
| 71 |
// on reconstruit la liste des applis. |
| 72 |
cd_do_reset_applications_list (); |
| 73 |
_browse_dir ("/usr/share/applications"); // oui c'est bourrin, ca ne doit pas arriver tres souvent? |
| 74 |
myData.pApplications = g_list_sort (myData.pApplications, (GCompareFunc) _compare_appli); |
| 75 |
} |
| 76 |
static void _browse_dir (const gchar *cDirPath) |
| 77 |
{ |
| 78 |
//\_______________ On ouvre le repertoire. |
| 79 |
GError *erreur = NULL; |
| 80 |
GDir *dir = g_dir_open (cDirPath, 0, &erreur); |
| 81 |
if (erreur != NULL) |
| 82 |
{ |
| 83 |
cd_warning (erreur->message); |
| 84 |
g_error_free (erreur); |
| 85 |
return ; |
| 86 |
} |
| 87 |
|
| 88 |
//\_______________ On liste tous ses .desktop. |
| 89 |
gboolean bFoundOneAppli = FALSE; |
| 90 |
gchar *cPath, *str, *cCommand, *cIconName; |
| 91 |
Icon *pIcon; |
| 92 |
GKeyFile* pKeyFile; |
| 93 |
const gchar *cFileName; |
| 94 |
do |
| 95 |
{ |
| 96 |
cFileName = g_dir_read_name (dir); |
| 97 |
if (cFileName == NULL) |
| 98 |
break ; |
| 99 |
|
| 100 |
cPath = g_strdup_printf ("%s/%s", cDirPath, cFileName); |
| 101 |
if (g_file_test (cPath, G_FILE_TEST_IS_DIR)) |
| 102 |
{ |
| 103 |
_browse_dir (cPath); |
| 104 |
g_free (cPath); |
| 105 |
} |
| 106 |
else |
| 107 |
{ |
| 108 |
pKeyFile = cairo_dock_open_key_file (cPath); |
| 109 |
if (pKeyFile == NULL) |
| 110 |
{ |
| 111 |
g_free (cPath); |
| 112 |
continue; |
| 113 |
} |
| 114 |
cCommand = g_key_file_get_string (pKeyFile, "Desktop Entry", "Exec", NULL); |
| 115 |
if (cCommand == NULL) |
| 116 |
{ |
| 117 |
g_key_file_free (pKeyFile); |
| 118 |
g_free (cPath); |
| 119 |
continue; |
| 120 |
} |
| 121 |
cIconName = g_key_file_get_string (pKeyFile, "Desktop Entry", "Icon", NULL); |
| 122 |
if (cIconName == NULL) |
| 123 |
{ |
| 124 |
g_key_file_free (pKeyFile); |
| 125 |
g_free (cPath); |
| 126 |
continue; |
| 127 |
} |
| 128 |
pIcon = cairo_dock_create_dummy_launcher (NULL, |
| 129 |
cIconName, |
| 130 |
cCommand, |
| 131 |
NULL, |
| 132 |
0); |
| 133 |
pIcon->cDesktopFileName = cPath; |
| 134 |
str = strchr (pIcon->cCommand, '%'); |
| 135 |
if (str != NULL) |
| 136 |
*str = '\0'; |
| 137 |
cd_debug (" + %s", pIcon->cCommand); |
| 138 |
pIcon->cWorkingDirectory = g_key_file_get_string (pKeyFile, "Desktop Entry", "Path", NULL); |
| 139 |
myData.pApplications = g_list_prepend (myData.pApplications, pIcon); |
| 140 |
g_key_file_free (pKeyFile); |
| 141 |
if (! bFoundOneAppli) |
| 142 |
bFoundOneAppli = TRUE; |
| 143 |
} |
| 144 |
} |
| 145 |
while (1); |
| 146 |
g_dir_close (dir); |
| 147 |
|
| 148 |
//\_______________ On le surveille s'il a fourni au moins une appli. |
| 149 |
if (bFoundOneAppli) |
| 150 |
{ |
| 151 |
gchar *cMonitoredPath = g_strdup (cDirPath); |
| 152 |
if (cairo_dock_fm_add_monitor_full (cMonitoredPath, TRUE, NULL, (CairoDockFMMonitorCallback) _cd_do_on_file_event, cMonitoredPath)) |
| 153 |
{ |
| 154 |
myData.pMonitorList = g_list_prepend (myData.pMonitorList, cMonitoredPath); |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
static int _same_command (Icon *pIcon1, Icon *pIcon2) |
| 159 |
{ |
| 160 |
return cairo_dock_strings_differ (pIcon1->cCommand, pIcon2->cCommand); |
| 161 |
} |
| 162 |
static int _similar_command (Icon *pIcon2, const gchar *cCommand) |
| 163 |
{ |
| 164 |
return (!pIcon2->cCommand || strncmp (cCommand, pIcon2->cCommand, strlen (cCommand))); |
| 165 |
} |
| 166 |
static gboolean _load_applis_buffer_idle (gpointer data) |
| 167 |
{ |
| 168 |
if (myData.pCurrentApplicationToLoad == NULL) |
| 169 |
{ |
| 170 |
myData.iSidLoadExternAppliIdle = 0; |
| 171 |
return FALSE; |
| 172 |
} |
| 173 |
int iNbAppliLoaded = 0; |
| 174 |
Icon *pIcon; |
| 175 |
gint iDesiredIconSize = 10; // cairo_dock_search_icon_size (GTK_ICON_SIZE_MENU); // 16px (was 48px but why?) // to data? |
| 176 |
GList *a; |
| 177 |
for (a = myData.pCurrentApplicationToLoad; a != NULL && iNbAppliLoaded < 3; a = a->next) // on en charge 3 d'un coup. |
| 178 |
{ |
| 179 |
pIcon = a->data; |
| 180 |
if (pIcon->image.pSurface == NULL) |
| 181 |
{ |
| 182 |
pIcon->fWidth = iDesiredIconSize; |
| 183 |
pIcon->fHeight = iDesiredIconSize; |
| 184 |
pIcon->fScale = 1.; |
| 185 |
gchar *cIconPath = cairo_dock_search_icon_s_path (pIcon->cFileName, iDesiredIconSize); |
| 186 |
cairo_dock_load_image_buffer(&pIcon->image, cIconPath, iDesiredIconSize, iDesiredIconSize, 0); |
| 187 |
g_free (cIconPath); |
| 188 |
iNbAppliLoaded ++; |
| 189 |
} |
| 190 |
} |
| 191 |
cd_debug (" %d de plus chargee(s)", iNbAppliLoaded); |
| 192 |
myData.pCurrentApplicationToLoad = a; |
| 193 |
if (a == NULL) // on est arrive au bout de la liste. |
| 194 |
{ |
| 195 |
cd_debug ("toutes les applis sont chargees !\n"); |
| 196 |
cairo_dock_redraw_container (CAIRO_CONTAINER (g_pMainDock)); |
| 197 |
myData.iSidLoadExternAppliIdle = 0; |
| 198 |
return FALSE; |
| 199 |
} |
| 200 |
else |
| 201 |
{ |
| 202 |
return TRUE; |
| 203 |
} |
| 204 |
} |
| 205 |
void cd_do_find_matching_applications (void) |
| 206 |
{ |
| 207 |
//\_______________ On liste les applis. |
| 208 |
if (myData.pApplications == NULL) // on n'a pas encore liste les applications, on le fait maintenant. |
| 209 |
{ |
| 210 |
_browse_dir ("/usr/share/applications"); |
| 211 |
myData.pApplications = g_list_sort (myData.pApplications, (GCompareFunc) _compare_appli); // on parcourt tout d'un coup (plutot que par exemple seulement les .desktop correspondant a la 1ere lettre car il y'a les sous-rep a parcourir, donc il faut de toute maniere se farcir la totale; de plus la commande peut differer du nom du .desktop. |
| 212 |
} |
| 213 |
|
| 214 |
if (myData.sCurrentText->len == 0) |
| 215 |
return ; |
| 216 |
|
| 217 |
//\_______________ On teste chaque appli qu'on rajoute a la liste si elle correspond. |
| 218 |
myData.pMatchingIcons = NULL; |
| 219 |
gboolean bMatch; |
| 220 |
Icon *pIcon; |
| 221 |
GList *a; |
| 222 |
for (a = myData.pApplications; a != NULL; a = a->next) |
| 223 |
{ |
| 224 |
pIcon = a->data; |
| 225 |
if (pIcon->cCommand == NULL) |
| 226 |
bMatch = FALSE; |
| 227 |
else |
| 228 |
{ |
| 229 |
bMatch = (g_ascii_strncasecmp (pIcon->cCommand, myData.sCurrentText->str, myData.sCurrentText->len) == 0); |
| 230 |
if (!bMatch) |
| 231 |
{ |
| 232 |
gchar *str = strchr (pIcon->cCommand, '-'); // on se limite au 1er tiret. |
| 233 |
if (str && *(str-1) != ' ') // on verifie qu'il n'est pas un tiret d'option |
| 234 |
{ |
| 235 |
str ++; |
| 236 |
bMatch = (g_ascii_strncasecmp (str, myData.sCurrentText->str, myData.sCurrentText->len) == 0); |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
if (!bMatch) |
| 242 |
{ |
| 243 |
/*if (bFound) |
| 244 |
break; |
| 245 |
else*/ |
| 246 |
continue; |
| 247 |
} |
| 248 |
if (g_list_find_custom (myData.pMatchingIcons, pIcon, (GCompareFunc)_same_command) == NULL) |
| 249 |
{ |
| 250 |
cd_debug (" on ajoute %s", pIcon->cCommand); |
| 251 |
myData.pMatchingIcons = g_list_prepend (myData.pMatchingIcons, pIcon); |
| 252 |
} |
| 253 |
} |
| 254 |
cd_debug (" -> found %d elements)", g_list_length (myData.pMatchingIcons)); |
| 255 |
myData.pMatchingIcons = g_list_reverse (myData.pMatchingIcons); |
| 256 |
|
| 257 |
//\_______________ On place l'appli preferee en premier. |
| 258 |
int i = *myData.sCurrentText->str - 'a'; |
| 259 |
if (i >= 0 && i < 26) |
| 260 |
{ |
| 261 |
gchar *cPrefferedAppli = myConfig.cPreferredApplis[i]; |
| 262 |
if (cPrefferedAppli != NULL && *cPrefferedAppli != '\0') |
| 263 |
{ |
| 264 |
cd_debug (" > une appli preferee est definie : '%s'", cPrefferedAppli); |
| 265 |
GList *ic = g_list_find_custom (myData.pMatchingIcons, cPrefferedAppli, (GCompareFunc) _similar_command); |
| 266 |
if (ic != NULL) |
| 267 |
{ |
| 268 |
cd_debug (" > on la passe en premier\n"); |
| 269 |
myData.pMatchingIcons = g_list_remove_link (myData.pMatchingIcons, ic); |
| 270 |
myData.pMatchingIcons = g_list_concat (ic, myData.pMatchingIcons); |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
//\_______________ On lance le chargement des icones en idle. |
| 276 |
myData.pCurrentApplicationToLoad = myData.pMatchingIcons; |
| 277 |
if (myData.iSidLoadExternAppliIdle == 0 && myData.pMatchingIcons != NULL) |
| 278 |
myData.iSidLoadExternAppliIdle = g_idle_add (_load_applis_buffer_idle, NULL); |
| 279 |
} |