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 #define __USE_POSIX
22 #include <time.h>
23 #include <signal.h>
24 
25 #include "applet-notes.h"
26 #include "tomboy-draw.h"
27 #include "tomboy-struct.h"
28 #include "tomboy-notifications.h"
29 
30 
31 CD_APPLET_ON_CLICK_BEGIN
32 	if (pClickedIcon != NULL && pClickedIcon != myIcon)
33 	{
34 		cd_message("tomboy : %s",pClickedIcon->cCommand);
35 		cd_notes_show_note (pClickedIcon->cCommand);
36 
37 		if (myData.iSidPopupDialog != 0)
38 		{
39 			g_source_remove (myData.iSidPopupDialog);
40 			myData.iSidPopupDialog = 0;
41 		}
42 		gldi_dialogs_remove_on_icon (pClickedIcon);
43 	}
44 	else if (pClickedIcon == myIcon && ! myData.bIsRunning)  // possible si on l'a quitte apres le demarrage de l'applet.
45 	{
46 		cd_notes_run_manager ();
47 	}
48 	else
49 		CD_APPLET_LEAVE (GLDI_NOTIFICATION_LET_PASS);
50 CD_APPLET_ON_CLICK_END
51 
52 
_add_note_and_show(const gchar * note_title)53 static void _add_note_and_show (const gchar *note_title)
54 {
55 	gchar *cNoteId = cd_notes_create_note (note_title);
56 	cd_debug (" %s -> %s", note_title, cNoteId);
57 	cd_notes_show_note (cNoteId);
58 	g_free (cNoteId);
59 }
_on_got_name(int iClickedButton,GtkWidget * pInteractiveWidget,gpointer data,CairoDialog * pDialog)60 static void _on_got_name (int iClickedButton, GtkWidget *pInteractiveWidget, gpointer data, CairoDialog *pDialog)
61 {
62 	CD_APPLET_ENTER;
63 	if (iClickedButton == 0 || iClickedButton == -1)  // ok button or Enter.
64 	{
65 		const gchar *note_title = gtk_entry_get_text (GTK_ENTRY (pInteractiveWidget));
66 		if (note_title != NULL)
67 			_add_note_and_show (note_title);
68 	}
69 	CD_APPLET_LEAVE ();
70 }
_cd_tomboy_create_new_note(void)71 static void _cd_tomboy_create_new_note (void)
72 {
73 	if (myConfig.bAutoNaming)
74 	{
75 		gchar *note_title = g_new0 (gchar, 50+1);
76 		time_t epoch = (time_t) time (NULL);
77 		struct tm currentTime;
78 		localtime_r (&epoch, &currentTime);
79 		strftime (note_title, 50, "%a-%d-%b_%r", &currentTime);
80 
81 		_add_note_and_show (note_title);
82 		g_free (note_title);
83 	}
84 	else
85 	{
86 		gldi_dialog_show_with_entry (D_("Note name : "),
87 			myIcon, myContainer,
88 			"same icon",
89 			NULL,
90 			(CairoDockActionOnAnswerFunc)_on_got_name,
91 			NULL, (GFreeFunc)NULL);
92 	}
93 }
_cd_tomboy_add_note(GtkMenuItem * menu_item,gpointer data)94 static void _cd_tomboy_add_note (GtkMenuItem *menu_item, gpointer data)
95 {
96 	_cd_tomboy_create_new_note ();
97 }
98 
_on_answer_delete(int iClickedButton,GtkWidget * pInteractiveWidget,const gchar * cCommand,CairoDialog * pDialog)99 static void _on_answer_delete (int iClickedButton, GtkWidget *pInteractiveWidget, const gchar *cCommand, CairoDialog *pDialog)
100 {
101 	CD_APPLET_ENTER;
102 	if (iClickedButton == 0 || iClickedButton == -1)  // ok button or Enter.
103 	{
104 		cd_notes_delete_note (cCommand);
105 	}
106 	CD_APPLET_LEAVE ();
107 }
_cd_tomboy_delete_note(GtkMenuItem * menu_item,Icon * pIcon)108 static void _cd_tomboy_delete_note (GtkMenuItem *menu_item, Icon *pIcon)
109 {
110 	g_return_if_fail (pIcon != NULL);
111 	if (myConfig.bAskBeforeDelete)
112 	{
113 		gchar *cQuestion = g_strdup_printf ("%s (%s)", D_("Delete this note?"), pIcon->cName);
114 		gldi_dialog_show_with_question (cQuestion,
115 			pIcon, myDock ? CAIRO_CONTAINER (myIcon->pSubDock) : myContainer,
116 			"same icon",
117 			(CairoDockActionOnAnswerFunc) _on_answer_delete,
118 			g_strdup (pIcon->cCommand), (GFreeFunc)g_free);
119 		g_free (cQuestion);
120 	}
121 	else
122 	{
123 		cd_notes_delete_note (pIcon->cCommand);
124 	}
125 }
126 
_cd_tomboy_reload_notes(GtkMenuItem * menu_item,gpointer data)127 static void _cd_tomboy_reload_notes (GtkMenuItem *menu_item, gpointer data)
128 {
129 	cd_notes_stop ();
130 
131 	cd_notes_start ();
132 }
133 
_on_active_search(int iClickedButton,GtkWidget * pInteractiveWidget,gpointer data,CairoDialog * pDialog)134 static void _on_active_search (int iClickedButton, GtkWidget *pInteractiveWidget, gpointer data, CairoDialog *pDialog)
135 {
136 	CD_APPLET_ENTER;
137 	if (iClickedButton == 0 || iClickedButton == -1)  // ok button or Enter.
138 	{
139 		const gchar *cContent = gtk_entry_get_text (GTK_ENTRY (pInteractiveWidget));
140 		if (cContent != NULL)
141 		{
142 			cd_tomboy_reset_icon_marks (FALSE);
143 			const gchar *cContents[2] = {cContent, NULL};
144 			GList *pList = cd_tomboy_find_notes_with_contents (cContents);
145 			cd_tomboy_show_results (pList);
146 			g_list_free (pList);
147 		}
148 	}
149 	CD_APPLET_LEAVE ();
150 }
_cd_tomboy_search_for_content(GtkMenuItem * menu_item,gpointer data)151 static void _cd_tomboy_search_for_content (GtkMenuItem *menu_item, gpointer data)
152 {
153 	gldi_dialog_show_with_entry (D_("Search for:"),
154 		myIcon,	myContainer,
155 		"same icon",
156 		NULL,
157 		(CairoDockActionOnAnswerFunc) _on_active_search, NULL, (GFreeFunc) NULL);
158 }
159 
_on_active_search_tag(int iClickedButton,GtkWidget * pInteractiveWidget,gpointer data,CairoDialog * pDialog)160 static void _on_active_search_tag (int iClickedButton, GtkWidget *pInteractiveWidget, gpointer data, CairoDialog *pDialog)
161 {
162 	CD_APPLET_ENTER;
163 	if (iClickedButton == 0 || iClickedButton == -1)  // ok button or Enter.
164 	{
165 		const gchar *cContent = gtk_entry_get_text (GTK_ENTRY (pInteractiveWidget));
166 		if (cContent != NULL)
167 		{
168 			cd_tomboy_reset_icon_marks (FALSE);
169 			GList *pList = cd_tomboy_find_notes_with_tag (cContent);
170 			cd_tomboy_show_results (pList);
171 			g_list_free (pList);
172 		}
173 	}
174 	CD_APPLET_LEAVE ();
175 }
_cd_tomboy_search_for_tag(GtkMenuItem * menu_item,gpointer data)176 static void _cd_tomboy_search_for_tag (GtkMenuItem *menu_item, gpointer data)
177 {
178 	gldi_dialog_show_with_entry (D_("Search for tag:"),
179 		myIcon,	myContainer,
180 		"same icon",
181 		NULL,
182 		(CairoDockActionOnAnswerFunc) _on_active_search_tag, NULL, (GFreeFunc) NULL);
183 }
184 
_cd_tomboy_search_for_today(GtkMenuItem * menu_item,gpointer data)185 static void _cd_tomboy_search_for_today (GtkMenuItem *menu_item, gpointer data)
186 {
187 	cd_tomboy_reset_icon_marks (FALSE);
188 	GList *pList = cd_tomboy_find_note_for_today ();
189 	cd_tomboy_show_results (pList);
190 	g_list_free (pList);
191 }
192 
_cd_tomboy_search_for_this_week(GtkMenuItem * menu_item,gpointer data)193 static void _cd_tomboy_search_for_this_week (GtkMenuItem *menu_item, gpointer data)
194 {
195 	cd_tomboy_reset_icon_marks (FALSE);
196 	GList *pList = cd_tomboy_find_note_for_this_week ();
197 	cd_tomboy_show_results (pList);
198 	g_list_free (pList);
199 }
200 
_cd_tomboy_search_for_next_week(GtkMenuItem * menu_item,gpointer data)201 static void _cd_tomboy_search_for_next_week (GtkMenuItem *menu_item, gpointer data)
202 {
203 	cd_tomboy_reset_icon_marks (FALSE);
204 	GList *pList = cd_tomboy_find_note_for_next_week ();
205 	cd_tomboy_show_results (pList);
206 	g_list_free (pList);
207 }
208 
_cd_tomboy_reset_marks(GtkMenuItem * menu_item,gpointer data)209 static void _cd_tomboy_reset_marks (GtkMenuItem *menu_item, gpointer data)
210 {
211 	cd_tomboy_reset_icon_marks (TRUE);
212 }
213 
214 CD_APPLET_ON_BUILD_MENU_BEGIN
215 	gboolean bClickOnNotes = (pClickedIcon !=  myIcon);
216 
217 	gchar *cLabel = g_strdup_printf ("%s (%s)", D_("Add a note"), D_("middle-click"));
218 	CD_APPLET_ADD_IN_MENU_WITH_STOCK (cLabel, GLDI_ICON_NAME_ADD, _cd_tomboy_add_note, CD_APPLET_MY_MENU);
219 	g_free (cLabel);
220 
221 	if (bClickOnNotes && pClickedIcon != NULL)
222 	{
223 		CD_APPLET_ADD_IN_MENU_WITH_STOCK_AND_DATA (D_("Delete this note"), GLDI_ICON_NAME_REMOVE, _cd_tomboy_delete_note, CD_APPLET_MY_MENU, pClickedIcon);
224 	}
225 
226 	CD_APPLET_ADD_IN_MENU_WITH_STOCK (D_("Reload notes"), GLDI_ICON_NAME_REFRESH, _cd_tomboy_reload_notes, CD_APPLET_MY_MENU);
227 
228 	CD_APPLET_ADD_SEPARATOR_IN_MENU (CD_APPLET_MY_MENU);
229 
230 	CD_APPLET_ADD_IN_MENU_WITH_STOCK  (D_("Search"), GLDI_ICON_NAME_FIND, _cd_tomboy_search_for_content, CD_APPLET_MY_MENU);
231 	CD_APPLET_ADD_IN_MENU(D_("Search for tag"), _cd_tomboy_search_for_tag, CD_APPLET_MY_MENU);
232 
233 	CD_APPLET_ADD_IN_MENU(D_("Search for today's note"), _cd_tomboy_search_for_today, CD_APPLET_MY_MENU);
234 	CD_APPLET_ADD_IN_MENU(D_("Search for this week's note"), _cd_tomboy_search_for_this_week, CD_APPLET_MY_MENU);
235 	CD_APPLET_ADD_IN_MENU(D_("Search for next week's note"), _cd_tomboy_search_for_next_week, CD_APPLET_MY_MENU);
236 
237 	GList *pList = (myDock ? (myIcon->pSubDock ? myIcon->pSubDock->icons : NULL) : myDesklet->icons);
238 	Icon *icon;
239 	GList *ic;
240 	for (ic = pList; ic != NULL; ic = ic->next)
241 	{
242 		icon = ic->data;
243 		if (icon->bHasIndicator)
244 		{
245 			CD_APPLET_ADD_IN_MENU_WITH_STOCK (D_("Reset marks"), GLDI_ICON_NAME_CLEAR, _cd_tomboy_reset_marks, CD_APPLET_MY_MENU);
246 			break ;
247 		}
248 	}
249 
250 	if (bClickOnNotes && pClickedIcon != NULL)
251 		CD_APPLET_LEAVE (GLDI_NOTIFICATION_INTERCEPT);
252 CD_APPLET_ON_BUILD_MENU_END
253 
254 
255 CD_APPLET_ON_MIDDLE_CLICK_BEGIN
256 	if (pClickedIcon == myIcon && ! myData.bIsRunning)  // possible si on l'a quitte apres le demarrage de l'applet.
257 	{
258 		cd_notes_run_manager ();
259 	}
260 	else
261 	{
262 		_cd_tomboy_create_new_note ();
263 	}
264 CD_APPLET_ON_MIDDLE_CLICK_END
265 
266 
267 
_popup_dialog(Icon * pIcon)268 static gboolean _popup_dialog (Icon *pIcon)
269 {
270 	CD_APPLET_ENTER;
271 	GldiContainer *pContainer = CD_APPLET_MY_ICONS_LIST_CONTAINER;
272 	if (pContainer->bInside)
273 	{
274 		if (g_list_find (CD_APPLET_MY_ICONS_LIST, pIcon))  // on verifie que l'icone ne s'est pas fait effacee entre-temps.
275 		{
276 			gchar *cDescription = pIcon->cClass && *pIcon->cClass != '\0' ?
277 				pIcon->cClass : D_("No description");
278 			gldi_dialog_show_temporary_with_icon (cDescription,
279 				pIcon,
280 				CD_APPLET_MY_ICONS_LIST_CONTAINER,
281 				myConfig.iDialogDuration,
282 				myConfig.cIconDefault != NULL ? myConfig.cIconDefault : MY_APPLET_SHARE_DATA_DIR"/"MY_APPLET_ICON_FILE);
283 		}
284 	}
285 	myData.iSidPopupDialog = 0;
286 	CD_APPLET_LEAVE (FALSE);
287 }
cd_tomboy_on_change_icon(gpointer pUserData,Icon * pIcon,CairoDock * pDock,gboolean * bStartAnimation)288 gboolean cd_tomboy_on_change_icon (gpointer pUserData, Icon *pIcon, CairoDock *pDock, gboolean *bStartAnimation)
289 {
290 	CD_APPLET_ENTER;
291 	if (myData.iSidPopupDialog != 0)
292 	{
293 		g_source_remove (myData.iSidPopupDialog);
294 		myData.iSidPopupDialog = 0;
295 	}
296 
297 	GList *pList = CD_APPLET_MY_ICONS_LIST;
298 	Icon *icon;
299 	GList *ic;
300 	for (ic = pList; ic != NULL; ic = ic->next)
301 	{
302 		icon = ic->data;
303 		gldi_dialogs_remove_on_icon (icon);
304 	}
305 
306 	if (pIcon && pIcon->bPointed)
307 	{
308 		myData.iSidPopupDialog = g_timeout_add (500, (GSourceFunc)_popup_dialog, pIcon);
309 	}
310 
311 	CD_APPLET_LEAVE (GLDI_NOTIFICATION_LET_PASS);
312 }
313 
cd_tomboy_on_leave_container(gpointer pUserData,GldiContainer * pContainer,gboolean * bStartAnimation)314 gboolean cd_tomboy_on_leave_container (gpointer pUserData, GldiContainer *pContainer, gboolean *bStartAnimation)
315 {
316 	CD_APPLET_ENTER;
317 	if (myData.iSidPopupDialog != 0)
318 	{
319 		g_source_remove (myData.iSidPopupDialog);
320 		myData.iSidPopupDialog = 0;
321 	}
322 
323 	GList *pList = CD_APPLET_MY_ICONS_LIST;
324 	Icon *icon;
325 	GList *ic;
326 	for (ic = pList; ic != NULL; ic = ic->next)
327 	{
328 		icon = ic->data;
329 		gldi_dialogs_remove_on_icon (icon);
330 	}
331 
332 	CD_APPLET_LEAVE (GLDI_NOTIFICATION_LET_PASS);
333 }
334