Debian Codegraph @

Scooby-Do/src/applet-backend-web.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-web.h"
32
33 // sub-listing
34
35 // fill entry
36 static gboolean _cd_do_fill_web_entry (CDEntry *pEntry);
37 // actions
38 static void _cd_do_web_search (CDEntry *pEntry);
39
40 //////////
41 // INIT //
42 //////////
43
44
45 ////////////////
46 // FILL ENTRY //
47 ////////////////
48
49 static gboolean _cd_do_fill_web_entry (CDEntry *pEntry)
50 {
51 if (pEntry->cIconName && pEntry->pIconSurface == NULL)
52 {
53 gchar *cImagePath = g_strconcat (MY_APPLET_SHARE_DATA_DIR, "/", pEntry->cIconName, NULL);
54 pEntry->pIconSurface = cairo_dock_create_surface_from_icon (cImagePath,
55 myDialogsParam.dialogTextDescription.iSize + 2,
56 myDialogsParam.dialogTextDescription.iSize + 2);
57 g_free (cImagePath);
58 return TRUE;
59 }
60 return FALSE;
61 }
62
63
64 /////////////
65 // ACTIONS //
66 /////////////
67
68 static void _cd_do_web_search (CDEntry *pEntry)
69 {
70 gchar *cEscapedText = g_uri_escape_string (myData.cSearchText ? myData.cSearchText : myData.sCurrentText->str,
71 "",
72 TRUE);
73 cd_debug ("cEscapedText : %s", cEscapedText);
74 gchar *cURI = g_strdup_printf (pEntry->cPath, cEscapedText);
75 cairo_dock_fm_launch_uri (cURI);
76 g_free (cURI);
77 g_free (cEscapedText);
78 }
79
80
81 /////////////////
82 // SUB-LISTING //
83 /////////////////
84
85
86 ////////////
87 // SEARCH //
88 ////////////
89
90 static GList* search (const gchar *cText, int iFilter, gboolean bSearchAll, int *iNbEntries)
91 {
92 cd_debug ("%s (%s)", __func__, cText);
93
94 GList *pEntries = NULL;
95 CDEntry *pEntry;
96
97 pEntry = g_new0 (CDEntry, 1);
98 pEntry->cPath = g_strdup ("http://en.wikipedia.org/w/index.php?title=Special:Search&go=Go&search=%s");
99 pEntry->cName = g_strdup (D_("Wikipedia"));
100 pEntry->cIconName = g_strdup ("wikipedia.png");
101 pEntry->fill = _cd_do_fill_web_entry;
102 pEntry->execute = _cd_do_web_search;
103 pEntries = g_list_prepend (pEntries, pEntry);
104
105 pEntry = g_new0 (CDEntry, 1);
106 pEntry->cPath = g_strdup ("http://search.yahoo.com/search?p=%s&ie=utf-8");
107 pEntry->cName = g_strdup (D_("Yahoo!"));
108 pEntry->cIconName = g_strdup ("yahoo.png");
109 pEntry->fill = _cd_do_fill_web_entry;
110 pEntry->execute = _cd_do_web_search;
111 pEntries = g_list_prepend (pEntries, pEntry);
112
113 pEntry = g_new0 (CDEntry, 1);
114 pEntry->cPath = g_strdup ("http://www.google.fr/search?q=%s&ie=utf-8");
115 pEntry->cName = g_strdup (D_("Google"));
116 pEntry->cIconName = g_strdup ("google.png");
117 pEntry->fill = _cd_do_fill_web_entry;
118 pEntry->execute = _cd_do_web_search;
119 pEntries = g_list_prepend (pEntries, pEntry);
120
121 if (! bSearchAll) // on arrete la, et gogol se retrouve en 1er.
122 {
123 pEntry = g_new0 (CDEntry, 1);
124 pEntry->cPath = g_strdup ("http://www.google.fr/search?q=%s&ie=utf-8");
125 pEntry->cName = g_strdup (D_("Search on the web"));
126 pEntry->cIconName = g_strdup ("internet.png");
127 pEntry->bMainEntry = TRUE;
128 pEntry->execute = _cd_do_web_search;
129 pEntry->fill = _cd_do_fill_web_entry;
130 pEntry->list = cd_do_list_main_sub_entry;
131 pEntries = g_list_prepend (pEntries, pEntry);
132
133 *iNbEntries = 4;
134 }
135 else // on liste les autres, qui se retrouvent en 1er.
136 {
137 pEntry = g_new0 (CDEntry, 1);
138 pEntry->cPath = g_strdup ("http://www.mediadico.com/dictionnaire/definition/%s/1");
139 pEntry->cName = g_strdup (D_("Mediadico"));
140 pEntry->cIconName = g_strdup ("mediadico.png");
141 pEntry->fill = _cd_do_fill_web_entry;
142 pEntry->execute = _cd_do_web_search;
143 pEntries = g_list_prepend (pEntries, pEntry);
144
145 pEntry = g_new0 (CDEntry, 1);
146 pEntry->cPath = g_strdup ("http://www.amazon.com/s?ie=UTF8&index=blended&link_code=qs&field-keywords=%s");
147 pEntry->cName = g_strdup (D_("Amazon"));
148 pEntry->cIconName = g_strdup ("amazon.png");
149 pEntry->fill = _cd_do_fill_web_entry;
150 pEntry->execute = _cd_do_web_search;
151 pEntries = g_list_prepend (pEntries, pEntry);
152
153 *iNbEntries = 5;
154 }
155
156 return pEntries;
157 }
158
159
160 //////////////
161 // REGISTER //
162 //////////////
163
164 void cd_do_register_web_backend (void)
165 {
166 CDBackend *pBackend = g_new0 (CDBackend, 1);
167 pBackend->cName = "Web";
168 pBackend->bIsThreaded = FALSE;
169 pBackend->bStaticResults = TRUE;
170 pBackend->init =(CDBackendInitFunc) NULL;
171 pBackend->search = (CDBackendSearchFunc) search;
172 myData.pBackends = g_list_prepend (myData.pBackends, pBackend);
173 }