Debian Codegraph @

Scooby-Do/src/applet-icon-finder.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
23 #include "applet-struct.h"
24 #include "applet-session.h"
25 #include "applet-appli-finder.h"
26 #include "applet-icon-finder.h"
27
28 static inline gboolean _cd_do_icon_match (Icon *pIcon, const gchar *cCommandPrefix, int length)
29 {
30 gboolean bMatch = FALSE;
31 if (pIcon->cBaseURI != NULL)
32 {
33 gchar *cFile = g_path_get_basename (pIcon->cCommand);
34 bMatch = (cFile && g_ascii_strncasecmp (cCommandPrefix, cFile, length) == 0);
35 g_free (cFile);
36 }
37 else if (pIcon->cCommand)
38 {
39 bMatch = (g_ascii_strncasecmp (cCommandPrefix, pIcon->cCommand, length) == 0);
40 if (!bMatch)
41 {
42 gchar *str = strchr (pIcon->cCommand, '-'); // on se limite au 1er tiret.
43 if (str && *(str-1) != ' ') // on verifie qu'il n'est pas un tiret d'option
44 {
45 str ++;
46 bMatch = (g_ascii_strncasecmp (str, cCommandPrefix, length) == 0);
47 }
48 if (!bMatch && pIcon->cName)
49 bMatch = (g_ascii_strncasecmp (cCommandPrefix, pIcon->cName, length) == 0);
50 }
51 }
52 return bMatch;
53 }
54
55
56 static inline void _cd_do_search_matching_icons_in_dock (CairoDock *pDock)
57 {
58 Icon *pIcon;
59 GList *ic;
60 for (ic = pDock->icons; ic != NULL; ic = ic->next)
61 {
62 pIcon = ic->data;
63 if (_cd_do_icon_match (pIcon, myData.sCurrentText->str, myData.sCurrentText->len))
64 {
65 myData.pMatchingIcons = g_list_prepend (myData.pMatchingIcons, pIcon);
66 }
67 }
68 }
69 static void _cd_do_search_in_one_dock (Icon *pIcon, gpointer data)
70 {
71 if (_cd_do_icon_match (pIcon, myData.sCurrentText->str, myData.sCurrentText->len))
72 {
73 myData.pMatchingIcons = g_list_prepend (myData.pMatchingIcons, pIcon);
74 }
75 }
76 void cd_do_search_matching_icons (void)
77 {
78 if (myData.sCurrentText->len == 0)
79 return;
80 cd_debug ("%s (%s)", __func__, myData.sCurrentText->str);
81 gchar *str = strchr (myData.sCurrentText->str, ' '); // on ne compte pas les arguments d'une eventuelle commande deja tapee.
82 guint length = myData.sCurrentText->len;
83 if (str != NULL)
84 {
85 g_string_set_size (myData.sCurrentText, str - myData.sCurrentText->str + 1);
86 cd_debug (" on ne cherchera que '%s' (len=%d)", myData.sCurrentText->str, myData.sCurrentText->len);
87 }
88
89 if (myData.pMatchingIcons == NULL)
90 {
91 cd_debug ("on cherche tout\n");
92 // on parcours tous les docks.
93 gldi_icons_foreach_in_docks ((GldiIconFunc) _cd_do_search_in_one_dock, NULL);
94 myData.pMatchingIcons = g_list_reverse (myData.pMatchingIcons);
95
96 // on rajoute les icones ne venant pas du dock.
97 cd_do_find_matching_applications ();
98 }
99 else // optimisation : on peut se contenter de chercher parmi les icones deja trouvees.
100 {
101 cd_debug ("on se contente d'enlever celles en trop\n");
102 GList *ic, *next_ic;
103 Icon *pIcon;
104 ic = myData.pMatchingIcons;
105 while (ic != NULL)
106 {
107 pIcon = ic->data;
108 next_ic = ic->next;
109 if (! _cd_do_icon_match (pIcon, myData.sCurrentText->str, myData.sCurrentText->len))
110 myData.pMatchingIcons = g_list_delete_link (myData.pMatchingIcons, ic);
111 ic = next_ic;
112 }
113 }
114 myData.pCurrentMatchingElement = myData.pMatchingIcons;
115 myData.iMatchingGlideCount = 0;
116 myData.iPreviousMatchingOffset = 0;
117 myData.iCurrentMatchingOffset = 0;
118 if (myData.pCurrentApplicationToLoad != NULL) // on va continuer le chargement sur la sous-liste.
119 myData.pCurrentApplicationToLoad = myData.pMatchingIcons; // comme l'ordre de la liste n'a pas ete altere, on n'est sur de ne pas sauter d'icone.
120 cairo_dock_redraw_container (CAIRO_CONTAINER (g_pMainDock));
121 //g_print ("%d / %d\n", length , myData.sCurrentText->len);
122 if (length != myData.sCurrentText->len)
123 g_string_set_size (myData.sCurrentText, length);
124 }
125
126
127 void cd_do_select_previous_next_matching_icon (gboolean bNext)
128 {
129 GList *pMatchingElement = myData.pCurrentMatchingElement;
130 do
131 {
132 if (!bNext)
133 myData.pCurrentMatchingElement = cairo_dock_get_previous_element (myData.pCurrentMatchingElement, myData.pMatchingIcons);
134 else
135 myData.pCurrentMatchingElement = cairo_dock_get_next_element (myData.pCurrentMatchingElement, myData.pMatchingIcons);
136 } while (myData.pCurrentMatchingElement != pMatchingElement && ((Icon*)myData.pCurrentMatchingElement->data)->image.pSurface == NULL);
137
138 if (myData.pCurrentMatchingElement != pMatchingElement) // on complete le texte et on redessine.
139 {
140 Icon *pIcon = myData.pCurrentMatchingElement->data;
141 if (pIcon->cCommand && *pIcon->cCommand != *myData.sCurrentText->str) // cas d'une commande avec un tiret.
142 myData.iNbValidCaracters = 0;
143 cd_do_delete_invalid_caracters ();
144
145 if (pIcon->cBaseURI != NULL)
146 {
147 gchar *cFile = g_path_get_basename (pIcon->cCommand);
148 g_string_assign (myData.sCurrentText, cFile);
149 g_free (cFile);
150 }
151 else
152 g_string_assign (myData.sCurrentText, pIcon->cCommand);
153
154
155 cd_do_load_pending_caracters ();
156
157 // on arme l'animation de decalage.
158 myData.iMatchingGlideCount = 10; // on rembobine l'animation.
159 myData.iPreviousMatchingOffset = myData.iCurrentMatchingOffset; // on part du point courant.
160 int iWidth, iHeight;
161 cairo_dock_get_icon_extent (pIcon, &iWidth, &iHeight);
162 if (iHeight != 0)
163 {
164 double fZoom = (double) g_pMainDock->container.iHeight/2 / iHeight;
165 myData.iMatchingAimPoint += (bNext ? 1 : -1) * iWidth * fZoom; // on cherche a atteindre le nouveau point.
166 }
167
168 // on repositionne les caracteres et on anime tout ca.
169 cd_do_launch_appearance_animation ();
170 cairo_dock_redraw_container (CAIRO_CONTAINER (g_pMainDock));
171 }
172 }