| 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 "cairo-dock.h" |
| 21 |
#include "interface-main-query.h" |
| 22 |
|
| 23 |
|
| 24 |
///////////// |
| 25 |
/// QUERY /// |
| 26 |
///////////// |
| 27 |
|
| 28 |
typedef struct { |
| 29 |
const gchar *cType; |
| 30 |
const gchar *cName; |
| 31 |
const gchar *cCommand; |
| 32 |
const gchar *cClass; |
| 33 |
const gchar *cContainerName; |
| 34 |
guint Xid; // ID of a window, as returned by gldi_window_get_id(); when using X, it's a 'Window' type |
| 35 |
const gchar *cDesktopFile; |
| 36 |
const gchar *cModuleName; |
| 37 |
gint iPosition; |
| 38 |
GList *pMatchingIcons; |
| 39 |
} CDQuery; |
| 40 |
|
| 41 |
static gboolean _prepare_query (CDQuery *pQuery, const gchar *cKey, const gchar *cValue) |
| 42 |
{ |
| 43 |
// init query |
| 44 |
memset (pQuery, 0, sizeof (CDQuery)); |
| 45 |
pQuery->iPosition = -1; |
| 46 |
|
| 47 |
g_return_val_if_fail (cKey != NULL, FALSE); |
| 48 |
if (cValue == NULL) // use "none" keyword to look for "icons that don't have key". |
| 49 |
return FALSE; |
| 50 |
|
| 51 |
if (strcmp (cKey, "name") == 0 || strcmp (cKey, "label") == 0) |
| 52 |
pQuery->cName = cValue; |
| 53 |
else if (strcmp (cKey, "command") == 0) |
| 54 |
pQuery->cCommand = cValue; |
| 55 |
else if (strcmp (cKey, "class") == 0) |
| 56 |
pQuery->cClass = cValue; |
| 57 |
else if (strcmp (cKey, "container") == 0) |
| 58 |
pQuery->cContainerName = cValue; |
| 59 |
else if (strcmp (cKey, "Xid") == 0) |
| 60 |
pQuery->Xid = strtol(cValue, NULL, 0); // can read hexa, decimal or octal. |
| 61 |
else if (strcmp (cKey, "config-file") == 0) |
| 62 |
pQuery->cDesktopFile = cValue; |
| 63 |
else if (strcmp (cKey, "module") == 0) |
| 64 |
pQuery->cModuleName = cValue; |
| 65 |
else if (strcmp (cKey, "position") == 0) |
| 66 |
pQuery->iPosition = atoi (cValue); |
| 67 |
else if (strcmp (cKey, "type") == 0) |
| 68 |
pQuery->cType = cValue; |
| 69 |
else |
| 70 |
{ |
| 71 |
cd_warning ("wrong key (%s)", cKey); |
| 72 |
return FALSE; |
| 73 |
} |
| 74 |
return TRUE; |
| 75 |
} |
| 76 |
|
| 77 |
static GList *_merge (GList *pList1, GList *pList2) |
| 78 |
{ |
| 79 |
//g_print ("%s ()\n", __func__); |
| 80 |
GList *pList = NULL; |
| 81 |
GList *ic; |
| 82 |
Icon *pIcon; |
| 83 |
for (ic = pList1; ic != NULL; ic = ic->next) |
| 84 |
{ |
| 85 |
pIcon = ic->data; |
| 86 |
if (g_list_find (pList2, pIcon) != NULL) |
| 87 |
pList = g_list_prepend (pList, pIcon); |
| 88 |
} |
| 89 |
g_list_free (pList1); |
| 90 |
g_list_free (pList2); |
| 91 |
return pList; |
| 92 |
} |
| 93 |
|
| 94 |
static GList *_concat (GList *pList1, GList *pList2) |
| 95 |
{ |
| 96 |
//g_print ("%s ()\n", __func__); |
| 97 |
GList *pList = g_list_copy (pList2); |
| 98 |
GList *ic; |
| 99 |
Icon *pIcon; |
| 100 |
for (ic = pList1; ic != NULL; ic = ic->next) |
| 101 |
{ |
| 102 |
pIcon = ic->data; |
| 103 |
if (!g_list_find (pList2, pIcon)) |
| 104 |
pList = g_list_prepend (pList, pIcon); |
| 105 |
} |
| 106 |
g_list_free (pList1); |
| 107 |
g_list_free (pList2); |
| 108 |
return pList; |
| 109 |
} |
| 110 |
|
| 111 |
static inline gboolean _strings_match (const gchar *q, const gchar *p) // query, parameter |
| 112 |
{ |
| 113 |
if (!p) |
| 114 |
return (strcmp (q, "none") == 0); |
| 115 |
int n = strlen(q); |
| 116 |
if (n != 0 && q[n-1] == '*') // ok with UTF-8 too. |
| 117 |
return (strncmp (q, p, n-1) == 0); |
| 118 |
return (strcmp (q, p) == 0); |
| 119 |
} |
| 120 |
|
| 121 |
static inline gboolean _strings_match_case (const gchar *q, const gchar *p) // query, parameter |
| 122 |
{ |
| 123 |
if (!p) |
| 124 |
return (strcmp (q, "none") == 0); |
| 125 |
int n = strlen(q); |
| 126 |
if (n != 0 && q[n-1] == '*') |
| 127 |
return (g_ascii_strncasecmp (q, p, n-1) == 0); |
| 128 |
return (g_ascii_strcasecmp (q, p) == 0); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
///////////////////// |
| 133 |
/// ICON MATCHING /// |
| 134 |
///////////////////// |
| 135 |
|
| 136 |
static gboolean _icon_is_matching (Icon *pIcon, CDQuery *pQuery) |
| 137 |
{ |
| 138 |
gboolean r; |
| 139 |
GldiContainer *pContainer = cairo_dock_get_icon_container (pIcon); |
| 140 |
if (CAIRO_DOCK_IS_DOCK (pContainer)) |
| 141 |
{ |
| 142 |
CairoDock *pDock = CAIRO_DOCK (pContainer); |
| 143 |
if (pDock->iRefCount > 0) |
| 144 |
{ |
| 145 |
Icon *pPointedIcon = cairo_dock_search_icon_pointing_on_dock (pDock, NULL); |
| 146 |
if (CAIRO_DOCK_IS_APPLET (pPointedIcon)) // ignore icons belonging to applets, because they should be managed by the applet only. |
| 147 |
return FALSE; |
| 148 |
} |
| 149 |
} |
| 150 |
else if (CAIRO_DOCK_IS_DESKLET (pContainer)) |
| 151 |
{ |
| 152 |
Icon *pMainIcon = CAIRO_DESKLET (pContainer)->pIcon; |
| 153 |
if (pIcon != pMainIcon) // same |
| 154 |
return FALSE; |
| 155 |
} |
| 156 |
|
| 157 |
if (pQuery->cType) |
| 158 |
{ |
| 159 |
if (strcmp (pQuery->cType, CD_TYPE_ICON) == 0) |
| 160 |
return TRUE; |
| 161 |
const gchar *cType; |
| 162 |
if (CAIRO_DOCK_ICON_TYPE_IS_LAUNCHER (pIcon)) |
| 163 |
cType = CD_TYPE_LAUNCHER; |
| 164 |
else if (CAIRO_DOCK_ICON_TYPE_IS_APPLI (pIcon)) |
| 165 |
cType = CD_TYPE_APPLICATION; |
| 166 |
else if (CAIRO_DOCK_ICON_TYPE_IS_APPLET (pIcon)) |
| 167 |
cType = CD_TYPE_APPLET; |
| 168 |
else if (CAIRO_DOCK_ICON_TYPE_IS_SEPARATOR (pIcon)) |
| 169 |
cType = CD_TYPE_SEPARATOR; |
| 170 |
else if (CAIRO_DOCK_ICON_TYPE_IS_CONTAINER (pIcon)) |
| 171 |
cType = CD_TYPE_STACK_ICON; |
| 172 |
else if (CAIRO_DOCK_ICON_TYPE_IS_CLASS_CONTAINER (pIcon)) |
| 173 |
cType = CD_TYPE_CLASS_ICON; |
| 174 |
else |
| 175 |
cType = CD_TYPE_ICON_OTHER; |
| 176 |
r = (strcmp (pQuery->cType, cType) == 0); |
| 177 |
if (r) return TRUE; // a query has only 1 key set. |
| 178 |
} |
| 179 |
if (pQuery->cName) |
| 180 |
{ |
| 181 |
r = _strings_match (pQuery->cName, pIcon->cName); |
| 182 |
if (r) return TRUE; |
| 183 |
} |
| 184 |
if (pQuery->cCommand) |
| 185 |
{ |
| 186 |
r = _strings_match (pQuery->cCommand, pIcon->cCommand); |
| 187 |
if (r) return TRUE; |
| 188 |
} |
| 189 |
if (pQuery->cClass) |
| 190 |
{ |
| 191 |
r = _strings_match_case (pQuery->cClass, pIcon->cClass); |
| 192 |
if (r) return TRUE; |
| 193 |
} |
| 194 |
if (pQuery->cContainerName) |
| 195 |
{ |
| 196 |
const gchar *cContainerName = NULL; |
| 197 |
if (CAIRO_DOCK_IS_DOCK (pContainer)) |
| 198 |
cContainerName = gldi_dock_get_name (CAIRO_DOCK(pContainer)); |
| 199 |
else if (CAIRO_DOCK_IS_DESKLET (pContainer)) |
| 200 |
{ |
| 201 |
Icon *pMainIcon = CAIRO_DESKLET (pContainer)->pIcon; |
| 202 |
if (CAIRO_DOCK_IS_APPLET (pMainIcon)) |
| 203 |
cContainerName = pMainIcon->pModuleInstance->pModule->pVisitCard->cModuleName; |
| 204 |
} |
| 205 |
r = _strings_match (pQuery->cContainerName, cContainerName); |
| 206 |
if (r) return TRUE; |
| 207 |
} |
| 208 |
if (pQuery->Xid != 0) |
| 209 |
{ |
| 210 |
r = (gldi_window_get_id(pIcon->pAppli) == pQuery->Xid); |
| 211 |
if (r) return TRUE; |
| 212 |
} |
| 213 |
if (pQuery->cDesktopFile) |
| 214 |
{ |
| 215 |
if (*pQuery->cDesktopFile == '/') // query the complete path. |
| 216 |
{ |
| 217 |
gchar *cDesktopFilePath = g_strdup_printf ("%s/%s", g_cCurrentLaunchersPath, pIcon->cDesktopFileName); |
| 218 |
r = _strings_match (pQuery->cDesktopFile, cDesktopFilePath); |
| 219 |
g_free (cDesktopFilePath); |
| 220 |
} |
| 221 |
else // query the file name only. |
| 222 |
{ |
| 223 |
r = _strings_match (pQuery->cDesktopFile, pIcon->cDesktopFileName); |
| 224 |
} |
| 225 |
/*if (!r && CAIRO_DOCK_IS_APPLET (pIcon) && pIcon->pModuleInstance->cConfFilePath) |
| 226 |
{ |
| 227 |
if (*pQuery->cDesktopFile == '/') // query the complete path. |
| 228 |
{ |
| 229 |
r = _strings_match (pQuery->cDesktopFile, pIcon->pModuleInstance->cConfFilePath); |
| 230 |
} |
| 231 |
else // query the file name only. |
| 232 |
{ |
| 233 |
gchar *str = strrchr (pIcon->pModuleInstance->cConfFilePath, '/'); |
| 234 |
if (str) |
| 235 |
r = _strings_match (pQuery->cDesktopFile, str+1); |
| 236 |
} |
| 237 |
}*/ |
| 238 |
if (r) return TRUE; |
| 239 |
} |
| 240 |
if (pQuery->cModuleName) |
| 241 |
{ |
| 242 |
r = (CAIRO_DOCK_IS_APPLET (pIcon) && _strings_match (pQuery->cModuleName, pIcon->pModuleInstance->pModule->pVisitCard->cModuleName)); |
| 243 |
if (r) return TRUE; |
| 244 |
} |
| 245 |
|
| 246 |
return FALSE; |
| 247 |
} |
| 248 |
|
| 249 |
static void _check_icon_matching (Icon *pIcon, CDQuery *pQuery) |
| 250 |
{ |
| 251 |
if (_icon_is_matching (pIcon, pQuery)) |
| 252 |
{ |
| 253 |
cd_debug ("found icon %s", pIcon->cName); |
| 254 |
pQuery->pMatchingIcons = g_list_prepend (pQuery->pMatchingIcons, pIcon); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
static void _get_icon_at_position_in_dock (const gchar *cDockName, CairoDock *pDock, CDQuery *pQuery) |
| 259 |
{ |
| 260 |
Icon *pIcon = g_list_nth_data (pDock->icons, pQuery->iPosition); |
| 261 |
if (pIcon != NULL) |
| 262 |
{ |
| 263 |
cd_debug ("found icon %s", pIcon->cName); |
| 264 |
pQuery->pMatchingIcons = g_list_prepend (pQuery->pMatchingIcons, pIcon); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
static gboolean _get_icon_at_position_in_desklet (CairoDesklet *pDesklet, CDQuery *pQuery) |
| 269 |
{ |
| 270 |
Icon *pIcon = g_list_nth_data (pDesklet->icons, pQuery->iPosition); |
| 271 |
if (pIcon != NULL) |
| 272 |
{ |
| 273 |
cd_debug ("found icon %s", pIcon->cName); |
| 274 |
pQuery->pMatchingIcons = g_list_prepend (pQuery->pMatchingIcons, pIcon); |
| 275 |
} |
| 276 |
return FALSE; // don't stop. |
| 277 |
} |
| 278 |
|
| 279 |
static GList *_find_matching_icons_for_key (const gchar *cKey, const gchar *cValue) |
| 280 |
{ |
| 281 |
//g_print (" %s (%s, %s)\n", __func__, cKey, cValue); |
| 282 |
CDQuery query; |
| 283 |
gboolean bValidQuery = _prepare_query (&query, cKey, cValue); |
| 284 |
g_return_val_if_fail (bValidQuery, NULL); |
| 285 |
|
| 286 |
if (query.iPosition >= 0) |
| 287 |
{ |
| 288 |
gldi_docks_foreach ((GHFunc) _get_icon_at_position_in_dock, &query); |
| 289 |
gldi_desklets_foreach ((GldiDeskletForeachFunc) _get_icon_at_position_in_desklet, &query); |
| 290 |
} |
| 291 |
else |
| 292 |
{ |
| 293 |
gldi_icons_foreach ((GldiIconFunc)_check_icon_matching, &query); |
| 294 |
} |
| 295 |
return query.pMatchingIcons; |
| 296 |
} |
| 297 |
static GList *_find_matching_icons_for_test (gchar *cTest) |
| 298 |
{ |
| 299 |
g_return_val_if_fail (cTest != NULL, NULL); |
| 300 |
//g_print (" %s (%s)\n", __func__, cTest); |
| 301 |
|
| 302 |
gchar *str = strchr (cTest, '='); |
| 303 |
g_return_val_if_fail (str != NULL, NULL); |
| 304 |
|
| 305 |
*str = '\0'; |
| 306 |
gchar *cKey = g_strstrip ((gchar*)cTest); // g_strstrip modifies the string in place (by moving the rest of the characters forward and cutting the trailing spaces) |
| 307 |
gchar *cValue = g_strstrip (str+1); |
| 308 |
|
| 309 |
return _find_matching_icons_for_key (cKey, cValue); |
| 310 |
} |
| 311 |
|
| 312 |
GList *cd_dbus_find_matching_icons (gchar *cQuery) |
| 313 |
{ |
| 314 |
g_return_val_if_fail (cQuery != NULL, NULL); |
| 315 |
|
| 316 |
gchar *str; |
| 317 |
str = strchr (cQuery, '|'); // a && b || c && d <=> (a && b) || (c && d) |
| 318 |
if (str) |
| 319 |
{ |
| 320 |
*str = '\0'; |
| 321 |
GList *pList1 = cd_dbus_find_matching_icons (cQuery); |
| 322 |
GList *pList2 = cd_dbus_find_matching_icons (str+1); |
| 323 |
return _concat (pList1, pList2); |
| 324 |
} |
| 325 |
str = strchr (cQuery, '&'); |
| 326 |
if (str) |
| 327 |
{ |
| 328 |
*str = '\0'; |
| 329 |
GList *pList1 = cd_dbus_find_matching_icons (cQuery); |
| 330 |
GList *pList2 = cd_dbus_find_matching_icons (str+1); |
| 331 |
return _merge (pList1, pList2); |
| 332 |
} |
| 333 |
return _find_matching_icons_for_test (cQuery); |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
////////////////////////// |
| 338 |
/// CONTAINER MATCHING /// |
| 339 |
////////////////////////// |
| 340 |
|
| 341 |
static gboolean _container_is_matching (GldiContainer *pContainer, const gchar *cContainerName, const gchar *cConfFile, CDQuery *pQuery) |
| 342 |
{ |
| 343 |
gboolean r; |
| 344 |
if (pQuery->cType) |
| 345 |
{ |
| 346 |
if (strcmp (pQuery->cType, "Container") == 0) |
| 347 |
return TRUE; |
| 348 |
const gchar *cType = ""; |
| 349 |
if (CAIRO_DOCK_IS_DOCK (pContainer)) |
| 350 |
cType = "Dock"; |
| 351 |
else if (CAIRO_DOCK_IS_DESKLET (pContainer)) |
| 352 |
cType = "Desklet"; |
| 353 |
r = (strcmp (pQuery->cType, cType) == 0); |
| 354 |
if (r) return TRUE; |
| 355 |
} |
| 356 |
if (pQuery->cName) |
| 357 |
{ |
| 358 |
r = _strings_match (pQuery->cName, cContainerName); |
| 359 |
if (r) return TRUE; |
| 360 |
} |
| 361 |
if (pQuery->cDesktopFile) |
| 362 |
{ |
| 363 |
if (*pQuery->cDesktopFile == '/') // path |
| 364 |
{ |
| 365 |
r = _strings_match (pQuery->cDesktopFile, cConfFile); |
| 366 |
} |
| 367 |
else |
| 368 |
{ |
| 369 |
const gchar *str = strrchr (cConfFile, '/'); |
| 370 |
r = _strings_match (pQuery->cDesktopFile, str+1); |
| 371 |
} |
| 372 |
if (r) return TRUE; |
| 373 |
} |
| 374 |
|
| 375 |
return FALSE ; |
| 376 |
} |
| 377 |
|
| 378 |
static void _check_dock_matching (const gchar *cDockName, CairoDock *pDock, CDQuery *pQuery) |
| 379 |
{ |
| 380 |
if (pDock->iRefCount > 0) // ignore sub-docks, as they are managed by the icon-container that holds it. |
| 381 |
return; |
| 382 |
gchar *cConfFile = NULL; |
| 383 |
if (pDock->bIsMainDock) |
| 384 |
cConfFile = g_strdup (g_cConfFile); |
| 385 |
else |
| 386 |
cConfFile = g_strdup_printf ("%s/%s.conf", g_cCurrentThemePath, cDockName); |
| 387 |
|
| 388 |
if (_container_is_matching (CAIRO_CONTAINER (pDock), cDockName, cConfFile, pQuery)) |
| 389 |
{ |
| 390 |
cd_debug ("found dock %s", cDockName); |
| 391 |
pQuery->pMatchingIcons = g_list_prepend (pQuery->pMatchingIcons, pDock); |
| 392 |
} |
| 393 |
|
| 394 |
g_free (cConfFile); |
| 395 |
} |
| 396 |
|
| 397 |
static gboolean _check_desklet_matching (CairoDesklet *pDesklet, CDQuery *pQuery) |
| 398 |
{ |
| 399 |
Icon *pIcon = pDesklet->pIcon; |
| 400 |
g_return_val_if_fail (CAIRO_DOCK_ICON_TYPE_IS_APPLET (pIcon), FALSE); |
| 401 |
|
| 402 |
const gchar *cConfFile = pIcon->pModuleInstance->cConfFilePath; |
| 403 |
const gchar *cName = pIcon->pModuleInstance->pModule->pVisitCard->cModuleName; |
| 404 |
|
| 405 |
if (_container_is_matching (CAIRO_CONTAINER (pDesklet), cName, cConfFile, pQuery)) |
| 406 |
{ |
| 407 |
cd_debug ("found desklet %s", cName); |
| 408 |
pQuery->pMatchingIcons = g_list_prepend (pQuery->pMatchingIcons, pDesklet); |
| 409 |
} |
| 410 |
|
| 411 |
return FALSE; // don't stop. |
| 412 |
} |
| 413 |
|
| 414 |
static GList *_find_matching_containers_for_key (const gchar *cKey, const gchar *cValue) |
| 415 |
{ |
| 416 |
//g_print (" %s (%s, %s)\n", __func__, cKey, cValue); |
| 417 |
CDQuery query; |
| 418 |
gboolean bValidQuery = _prepare_query (&query, cKey, cValue); |
| 419 |
g_return_val_if_fail (bValidQuery, NULL); |
| 420 |
|
| 421 |
gldi_docks_foreach ((GHFunc) _check_dock_matching, &query); |
| 422 |
gldi_desklets_foreach ((GldiDeskletForeachFunc) _check_desklet_matching, &query); |
| 423 |
|
| 424 |
return query.pMatchingIcons; |
| 425 |
} |
| 426 |
static GList *_find_matching_containers_for_test (gchar *cTest) |
| 427 |
{ |
| 428 |
g_return_val_if_fail (cTest != NULL, NULL); |
| 429 |
//g_print (" %s (%s)\n", __func__, cTest); |
| 430 |
|
| 431 |
gchar *str = strchr (cTest, '='); |
| 432 |
g_return_val_if_fail (str != NULL, NULL); |
| 433 |
|
| 434 |
*str = '\0'; |
| 435 |
gchar *cKey = g_strstrip ((gchar*)cTest); // g_strstrip modifies the string in place (by moving the rest of the characters forward and cutting the trailing spaces) |
| 436 |
gchar *cValue = g_strstrip (str+1); |
| 437 |
|
| 438 |
return _find_matching_containers_for_key (cKey, cValue); |
| 439 |
} |
| 440 |
|
| 441 |
GList *cd_dbus_find_matching_containers (gchar *cQuery) |
| 442 |
{ |
| 443 |
g_return_val_if_fail (cQuery != NULL, NULL); |
| 444 |
//g_print ("%s (%s)\n", __func__, cQuery); |
| 445 |
|
| 446 |
gchar *str; |
| 447 |
str = strchr (cQuery, '|'); // a && b || c && d <=> (a && b) || (c && d) |
| 448 |
if (str) |
| 449 |
{ |
| 450 |
*str = '\0'; |
| 451 |
GList *pList1 = cd_dbus_find_matching_containers (cQuery); |
| 452 |
GList *pList2 = cd_dbus_find_matching_containers (str+1); |
| 453 |
return _concat (pList1, pList2); |
| 454 |
} |
| 455 |
str = strchr (cQuery, '&'); |
| 456 |
if (str) |
| 457 |
{ |
| 458 |
*str = '\0'; |
| 459 |
GList *pList1 = cd_dbus_find_matching_containers (cQuery); |
| 460 |
GList *pList2 = cd_dbus_find_matching_containers (str+1); |
| 461 |
return _merge (pList1, pList2); |
| 462 |
} |
| 463 |
return _find_matching_containers_for_test (cQuery); |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
/////////////////////// |
| 469 |
/// MODULE MATCHING /// |
| 470 |
/////////////////////// |
| 471 |
static gboolean _add_module (const gchar *cModuleName, GldiModule *pModule, CDQuery *pQuery) |
| 472 |
{ |
| 473 |
pQuery->pMatchingIcons = g_list_prepend (pQuery->pMatchingIcons, pModule); |
| 474 |
return FALSE; // don't stop. |
| 475 |
} |
| 476 |
static void _add_manager (GldiManager *pManager, CDQuery *pQuery) |
| 477 |
{ |
| 478 |
pQuery->pMatchingIcons = g_list_prepend (pQuery->pMatchingIcons, pManager); |
| 479 |
} |
| 480 |
static GList *_find_matching_modules_for_key (const gchar *cKey, const gchar *cValue) |
| 481 |
{ |
| 482 |
//g_print (" %s (%s, %s)\n", __func__, cKey, cValue); |
| 483 |
CDQuery query; |
| 484 |
gboolean bValidQuery = _prepare_query (&query, cKey, cValue); |
| 485 |
g_return_val_if_fail (bValidQuery, NULL); |
| 486 |
|
| 487 |
if (query.cType) |
| 488 |
{ |
| 489 |
if (strcmp (query.cType, CD_TYPE_MODULE) == 0) |
| 490 |
gldi_module_foreach ((GHRFunc)_add_module, &query); |
| 491 |
else if (strcmp (query.cType, CD_TYPE_MANAGER) == 0) |
| 492 |
gldi_managers_foreach ((GFunc)_add_manager, &query); |
| 493 |
} |
| 494 |
|
| 495 |
if (query.cName) // the only relevant way to identify a module/maanger is by name, so we just need to look for the given name. |
| 496 |
{ |
| 497 |
GldiModule *pModule = gldi_module_get (query.cName); |
| 498 |
if (pModule != NULL) |
| 499 |
{ |
| 500 |
cd_debug ("found module %s", pModule->pVisitCard->cModuleName); |
| 501 |
query.pMatchingIcons = g_list_prepend (query.pMatchingIcons, pModule); |
| 502 |
} |
| 503 |
else |
| 504 |
{ |
| 505 |
GldiManager *pManager = gldi_manager_get (query.cName); |
| 506 |
if (pManager != NULL) |
| 507 |
{ |
| 508 |
cd_debug ("found manager %s", pManager->cModuleName); |
| 509 |
query.pMatchingIcons = g_list_prepend (query.pMatchingIcons, pManager); |
| 510 |
} |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
return query.pMatchingIcons; |
| 515 |
} |
| 516 |
static GList *_find_matching_modules_for_test (gchar *cTest) |
| 517 |
{ |
| 518 |
g_return_val_if_fail (cTest != NULL, NULL); |
| 519 |
//g_print (" %s (%s)\n", __func__, cTest); |
| 520 |
|
| 521 |
gchar *str = strchr (cTest, '='); |
| 522 |
g_return_val_if_fail (str != NULL, NULL); |
| 523 |
|
| 524 |
*str = '\0'; |
| 525 |
gchar *cKey = g_strstrip ((gchar*)cTest); // g_strstrip modifies the string in place (by moving the rest of the characters forward and cutting the trailing spaces) |
| 526 |
gchar *cValue = g_strstrip (str+1); |
| 527 |
|
| 528 |
return _find_matching_modules_for_key (cKey, cValue); |
| 529 |
} |
| 530 |
|
| 531 |
GList *cd_dbus_find_matching_modules (gchar *cQuery) |
| 532 |
{ |
| 533 |
g_return_val_if_fail (cQuery != NULL, NULL); |
| 534 |
//g_print ("%s (%s)\n", __func__, cQuery); |
| 535 |
|
| 536 |
gchar *str; |
| 537 |
str = strchr (cQuery, '|'); // a && b || c && d <=> (a && b) || (c && d) |
| 538 |
if (str) |
| 539 |
{ |
| 540 |
*str = '\0'; |
| 541 |
GList *pList1 = cd_dbus_find_matching_modules (cQuery); |
| 542 |
GList *pList2 = cd_dbus_find_matching_modules (str+1); |
| 543 |
return _concat (pList1, pList2); |
| 544 |
} |
| 545 |
str = strchr (cQuery, '&'); |
| 546 |
if (str) |
| 547 |
{ |
| 548 |
*str = '\0'; |
| 549 |
GList *pList1 = cd_dbus_find_matching_modules (cQuery); |
| 550 |
GList *pList2 = cd_dbus_find_matching_modules (str+1); |
| 551 |
return _merge (pList1, pList2); |
| 552 |
} |
| 553 |
return _find_matching_modules_for_test (cQuery); |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
//////////////////////////////// |
| 558 |
/// MODULE INSTANCE MATCHING /// |
| 559 |
//////////////////////////////// |
| 560 |
|
| 561 |
static gboolean _module_instance_is_matching (GldiModuleInstance *pModuleInstance, CDQuery *pQuery) |
| 562 |
{ |
| 563 |
gboolean r; |
| 564 |
|
| 565 |
if (pQuery->cType && strcmp (pQuery->cType, CD_TYPE_MODULE_INSTANCE) == 0) |
| 566 |
return TRUE; |
| 567 |
|
| 568 |
if (pQuery->cModuleName) |
| 569 |
{ |
| 570 |
r = _strings_match (pQuery->cModuleName, pModuleInstance->pModule->pVisitCard->cModuleName); |
| 571 |
if (r) return TRUE; |
| 572 |
} |
| 573 |
if (pQuery->cDesktopFile && pModuleInstance->cConfFilePath) |
| 574 |
{ |
| 575 |
if (*pQuery->cDesktopFile == '/') // path |
| 576 |
{ |
| 577 |
r = _strings_match (pQuery->cDesktopFile, pModuleInstance->cConfFilePath); |
| 578 |
} |
| 579 |
else |
| 580 |
{ |
| 581 |
const gchar *str = strrchr (pModuleInstance->cConfFilePath, '/'); |
| 582 |
r = _strings_match (pQuery->cDesktopFile, str+1); |
| 583 |
} |
| 584 |
if (r) return TRUE; |
| 585 |
} |
| 586 |
|
| 587 |
return FALSE; |
| 588 |
} |
| 589 |
|
| 590 |
static gboolean _check_module_instance_matching (const gchar *cModuleName, GldiModule *pModule, CDQuery *pQuery) |
| 591 |
{ |
| 592 |
GldiModuleInstance *pInstance; |
| 593 |
GList *mi; |
| 594 |
for (mi = pModule->pInstancesList; mi != NULL; mi = mi->next) |
| 595 |
{ |
| 596 |
pInstance = mi->data; |
| 597 |
if (_module_instance_is_matching (pInstance, pQuery)) |
| 598 |
{ |
| 599 |
cd_debug ("found module instance %s", pInstance->pModule->pVisitCard->cModuleName); |
| 600 |
pQuery->pMatchingIcons = g_list_prepend (pQuery->pMatchingIcons, pInstance); |
| 601 |
} |
| 602 |
} |
| 603 |
return FALSE; // don't stop. |
| 604 |
} |
| 605 |
|
| 606 |
static GList *_find_matching_module_instances_for_key (const gchar *cKey, const gchar *cValue) |
| 607 |
{ |
| 608 |
//g_print (" %s (%s, %s)\n", __func__, cKey, cValue); |
| 609 |
CDQuery query; |
| 610 |
gboolean bValidQuery = _prepare_query (&query, cKey, cValue); |
| 611 |
g_return_val_if_fail (bValidQuery, NULL); |
| 612 |
|
| 613 |
gldi_module_foreach ((GHRFunc)_check_module_instance_matching, &query); |
| 614 |
|
| 615 |
return query.pMatchingIcons; |
| 616 |
} |
| 617 |
static GList *_find_matching_module_instances_for_test (gchar *cTest) |
| 618 |
{ |
| 619 |
g_return_val_if_fail (cTest != NULL, NULL); |
| 620 |
//g_print (" %s (%s)\n", __func__, cTest); |
| 621 |
|
| 622 |
gchar *str = strchr (cTest, '='); |
| 623 |
g_return_val_if_fail (str != NULL, NULL); |
| 624 |
|
| 625 |
*str = '\0'; |
| 626 |
gchar *cKey = g_strstrip ((gchar*)cTest); // g_strstrip modifies the string in place (by moving the rest of the characters forward and cutting the trailing spaces) |
| 627 |
gchar *cValue = g_strstrip (str+1); |
| 628 |
|
| 629 |
return _find_matching_module_instances_for_key (cKey, cValue); |
| 630 |
} |
| 631 |
|
| 632 |
GList *cd_dbus_find_matching_module_instances (gchar *cQuery) |
| 633 |
{ |
| 634 |
g_return_val_if_fail (cQuery != NULL, NULL); |
| 635 |
//g_print ("%s (%s)\n", __func__, cQuery); |
| 636 |
|
| 637 |
gchar *str; |
| 638 |
str = strchr (cQuery, '|'); // a && b || c && d <=> (a && b) || (c && d) |
| 639 |
if (str) |
| 640 |
{ |
| 641 |
*str = '\0'; |
| 642 |
GList *pList1 = cd_dbus_find_matching_module_instances (cQuery); |
| 643 |
GList *pList2 = cd_dbus_find_matching_module_instances (str+1); |
| 644 |
return _concat (pList1, pList2); |
| 645 |
} |
| 646 |
str = strchr (cQuery, '&'); |
| 647 |
if (str) |
| 648 |
{ |
| 649 |
*str = '\0'; |
| 650 |
GList *pList1 = cd_dbus_find_matching_module_instances (cQuery); |
| 651 |
GList *pList2 = cd_dbus_find_matching_module_instances (str+1); |
| 652 |
return _merge (pList1, pList2); |
| 653 |
} |
| 654 |
return _find_matching_module_instances_for_test (cQuery); |
| 655 |
} |
| 656 |
|
| 657 |
|
| 658 |
CDMainType cd_dbus_get_main_type (const gchar *cType, int n) |
| 659 |
{ |
| 660 |
if (!cType) |
| 661 |
return CD_MAIN_TYPE_UNKNOWN; |
| 662 |
if (n <= 0) |
| 663 |
n = strlen (cType); |
| 664 |
|
| 665 |
if (strncmp (cType, CD_TYPE_ICON, n) == 0 |
| 666 |
|| strncmp (cType, CD_TYPE_LAUNCHER, n) == 0 |
| 667 |
|| strncmp (cType, CD_TYPE_APPLICATION, n) == 0 |
| 668 |
|| strncmp (cType, CD_TYPE_APPLET, n) == 0 |
| 669 |
|| strncmp (cType, CD_TYPE_SEPARATOR, n) == 0 |
| 670 |
|| strncmp (cType, CD_TYPE_STACK_ICON, n) == 0 |
| 671 |
|| strncmp (cType, CD_TYPE_CLASS_ICON, n) == 0 |
| 672 |
|| strncmp (cType, CD_TYPE_ICON_OTHER, n) == 0) |
| 673 |
{ |
| 674 |
return CD_MAIN_TYPE_ICON; |
| 675 |
} |
| 676 |
else if (strncmp (cType, CD_TYPE_CONTAINER, n) == 0 |
| 677 |
|| strncmp (cType, CD_TYPE_DOCK, n) == 0 |
| 678 |
|| strncmp (cType, CD_TYPE_DESKLET, n) == 0) |
| 679 |
{ |
| 680 |
return CD_MAIN_TYPE_CONTAINER; |
| 681 |
} |
| 682 |
else if (strncmp (cType, CD_TYPE_MODULE, n) == 0 |
| 683 |
|| strncmp (cType, CD_TYPE_MANAGER, n) == 0) |
| 684 |
{ |
| 685 |
return CD_MAIN_TYPE_MODULE; |
| 686 |
} |
| 687 |
else if (strncmp (cType, CD_TYPE_MODULE_INSTANCE, n) == 0) |
| 688 |
{ |
| 689 |
return CD_MAIN_TYPE_MODULE_INSTANCE; |
| 690 |
} |
| 691 |
else // wrong type |
| 692 |
{ |
| 693 |
return CD_MAIN_TYPE_UNKNOWN; |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
GList *cd_dbus_find_matching_objects (gchar *cQuery) |
| 698 |
{ |
| 699 |
CDMainType iType = CD_MAIN_TYPE_UNKNOWN; |
| 700 |
GList *pObjects = NULL; |
| 701 |
gchar *str = strstr (cQuery, "type"); |
| 702 |
if (str) |
| 703 |
{ |
| 704 |
str = strchr (str+4, '='); // jump to '=' |
| 705 |
if (str) |
| 706 |
{ |
| 707 |
str ++; // skip it |
| 708 |
while (*str == ' ') str ++; // skip spaces |
| 709 |
gchar *end = str+1; |
| 710 |
while (*end != '\0' && *end != ' ' && *end != '&' && *end != '|') end ++; // go to the end of the value |
| 711 |
int n = end - str; |
| 712 |
iType = cd_dbus_get_main_type (str, n); |
| 713 |
} |
| 714 |
} |
| 715 |
switch (iType) |
| 716 |
{ |
| 717 |
case CD_MAIN_TYPE_ICON: |
| 718 |
pObjects = cd_dbus_find_matching_icons (cQuery); |
| 719 |
break; |
| 720 |
|
| 721 |
case CD_MAIN_TYPE_CONTAINER: |
| 722 |
pObjects = cd_dbus_find_matching_containers (cQuery); |
| 723 |
break; |
| 724 |
case CD_MAIN_TYPE_MODULE: |
| 725 |
pObjects = cd_dbus_find_matching_modules (cQuery); |
| 726 |
break; |
| 727 |
case CD_MAIN_TYPE_MODULE_INSTANCE: |
| 728 |
pObjects = cd_dbus_find_matching_module_instances (cQuery); |
| 729 |
break; |
| 730 |
default: |
| 731 |
{ |
| 732 |
gchar *query = g_strdup (cQuery); |
| 733 |
GList *o = cd_dbus_find_matching_icons (query); |
| 734 |
pObjects = o; |
| 735 |
|
| 736 |
memcpy (query, cQuery, strlen (cQuery)); |
| 737 |
o = cd_dbus_find_matching_containers (query); |
| 738 |
pObjects = g_list_concat (pObjects, o); |
| 739 |
|
| 740 |
memcpy (query, cQuery, strlen (cQuery)); |
| 741 |
o = cd_dbus_find_matching_modules (query); |
| 742 |
pObjects = g_list_concat (pObjects, o); |
| 743 |
|
| 744 |
memcpy (query, cQuery, strlen (cQuery)); |
| 745 |
o = cd_dbus_find_matching_module_instances (query); |
| 746 |
pObjects = g_list_concat (pObjects, o); |
| 747 |
g_free (query); |
| 748 |
} |
| 749 |
break; |
| 750 |
} |
| 751 |
return pObjects; |
| 752 |
} |