Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #ifdef ENABLE_QUICKSTART_APPLET
21 :
22 : #include <unotools/moduleoptions.hxx>
23 :
24 : #include <unotools/dynamicmenuoptions.hxx>
25 :
26 : #include <gtk/gtk.h>
27 : #include <glib.h>
28 : #include <osl/mutex.hxx>
29 : #include <vcl/bitmapex.hxx>
30 : #include <vcl/bmpacc.hxx>
31 : #include "tools/rc.hxx"
32 : #include <sfx2/app.hxx>
33 : #include "app.hrc"
34 : #ifndef __SHUTDOWNICON_HXX__
35 : #define USE_APP_SHORTCUTS
36 : #include "shutdownicon.hxx"
37 : #endif
38 :
39 : #ifdef ENABLE_GIO
40 : #include <gio/gio.h>
41 : #endif
42 :
43 : // Cut/paste from vcl/inc/svids.hrc
44 : #define SV_ICON_SMALL_START 25000
45 :
46 : #define SV_ICON_ID_OFFICE 1
47 : #define SV_ICON_ID_TEXT 2
48 : #define SV_ICON_ID_SPREADSHEET 4
49 : #define SV_ICON_ID_DRAWING 6
50 : #define SV_ICON_ID_PRESENTATION 8
51 : #define SV_ICON_ID_TEMPLATE 11
52 : #define SV_ICON_ID_DATABASE 12
53 : #define SV_ICON_ID_FORMULA 13
54 :
55 : using namespace ::rtl;
56 : using namespace ::osl;
57 :
58 : static ResMgr *pVCLResMgr;
59 : static GtkStatusIcon* pTrayIcon;
60 : static GtkWidget *pExitMenuItem = NULL;
61 : static GtkWidget *pOpenMenuItem = NULL;
62 : static GtkWidget *pDisableMenuItem = NULL;
63 : #ifdef ENABLE_GIO
64 : GFileMonitor* pMonitor = NULL;
65 : #endif
66 :
67 0 : static void open_url_cb( GtkWidget *, gpointer data )
68 : {
69 : ShutdownIcon::OpenURL( *(OUString *)data,
70 0 : OUString( "_default" ) );
71 0 : }
72 :
73 0 : static void open_file_cb( GtkWidget * )
74 : {
75 0 : if ( !ShutdownIcon::bModalMode )
76 0 : ShutdownIcon::FileOpen();
77 0 : }
78 :
79 0 : static void open_template_cb( GtkWidget * )
80 : {
81 0 : if ( !ShutdownIcon::bModalMode )
82 0 : ShutdownIcon::FromTemplate();
83 0 : }
84 :
85 0 : static void systray_disable_cb()
86 : {
87 0 : ShutdownIcon::SetAutostart( false );
88 0 : ShutdownIcon::terminateDesktop();
89 0 : }
90 :
91 0 : static void exit_quickstarter_cb( GtkWidget * )
92 : {
93 0 : plugin_shutdown_sys_tray();
94 : //terminate may cause this .so to be unloaded. So we must be hands off
95 : //all calls into this .so after this call
96 0 : ShutdownIcon::terminateDesktop();
97 0 : }
98 :
99 0 : static void menu_deactivate_cb( GtkWidget *pMenu )
100 : {
101 0 : gtk_menu_popdown( GTK_MENU( pMenu ) );
102 0 : }
103 :
104 0 : static GdkPixbuf * ResIdToPixbuf( sal_uInt16 nResId )
105 : {
106 0 : ResId aResId( nResId, *pVCLResMgr );
107 0 : BitmapEx aIcon( aResId );
108 0 : Bitmap pInSalBitmap = aIcon.GetBitmap();
109 0 : AlphaMask pInSalAlpha = aIcon.GetAlpha();
110 :
111 0 : if( pInSalBitmap.GetBitCount() != 24 )
112 0 : pInSalBitmap.Convert( BMP_CONVERSION_24BIT );
113 :
114 0 : Bitmap::ScopedReadAccess pSalBitmap(pInSalBitmap);
115 0 : AlphaMask::ScopedReadAccess pSalAlpha(pInSalAlpha);
116 :
117 0 : g_return_val_if_fail( pSalBitmap, NULL );
118 :
119 0 : Size aSize( pSalBitmap->Width(), pSalBitmap->Height() );
120 0 : if (pSalAlpha)
121 0 : g_return_val_if_fail( Size( pSalAlpha->Width(), pSalAlpha->Height() ) == aSize, NULL );
122 :
123 : int nX, nY;
124 0 : guchar *pPixbufData = ( guchar * )g_malloc( 4 * aSize.Width() * aSize.Height() );
125 0 : guchar *pDestData = pPixbufData;
126 :
127 0 : for( nY = 0; nY < pSalBitmap->Height(); nY++ )
128 : {
129 0 : for( nX = 0; nX < pSalBitmap->Width(); nX++ )
130 : {
131 0 : BitmapColor aPix;
132 0 : aPix = pSalBitmap->GetPixel( nY, nX );
133 0 : pDestData[0] = aPix.GetRed();
134 0 : pDestData[1] = aPix.GetGreen();
135 0 : pDestData[2] = aPix.GetBlue();
136 0 : if (pSalAlpha)
137 : {
138 0 : aPix = pSalAlpha->GetPixel( nY, nX );
139 0 : pDestData[3] = 255 - aPix.GetIndex();
140 : }
141 : else
142 0 : pDestData[3] = 255;
143 0 : pDestData += 4;
144 0 : }
145 : }
146 :
147 : return gdk_pixbuf_new_from_data( pPixbufData,
148 : GDK_COLORSPACE_RGB, sal_True, 8,
149 0 : aSize.Width(), aSize.Height(),
150 0 : aSize.Width() * 4,
151 : (GdkPixbufDestroyNotify) g_free,
152 0 : NULL );
153 : }
154 :
155 : extern "C" {
156 0 : static void oustring_delete (gpointer data,
157 : GClosure * /* closure */)
158 : {
159 0 : OUString *pURL = (OUString *) data;
160 0 : delete pURL;
161 0 : }
162 : }
163 :
164 0 : static void add_item( GtkMenuShell *pMenuShell, const char *pAsciiURL,
165 : OUString *pOverrideLabel,
166 : sal_uInt16 nResId, GCallback pFnCallback )
167 : {
168 : OUString *pURL = new OUString (OStringToOUString( pAsciiURL,
169 0 : RTL_TEXTENCODING_UTF8 ));
170 0 : OString aLabel;
171 0 : if (pOverrideLabel)
172 0 : aLabel = OUStringToOString (*pOverrideLabel, RTL_TEXTENCODING_UTF8);
173 : else
174 : {
175 0 : ShutdownIcon *pShutdownIcon = ShutdownIcon::getInstance();
176 0 : aLabel = OUStringToOString (pShutdownIcon->GetUrlDescription( *pURL ),
177 0 : RTL_TEXTENCODING_UTF8);
178 : }
179 :
180 0 : GdkPixbuf *pPixbuf = ResIdToPixbuf( SV_ICON_SMALL_START + nResId );
181 0 : GtkWidget *pImage = gtk_image_new_from_pixbuf( pPixbuf );
182 0 : g_object_unref( G_OBJECT( pPixbuf ) );
183 :
184 0 : GtkWidget *pMenuItem = gtk_image_menu_item_new_with_label( aLabel.getStr() );
185 0 : gtk_image_menu_item_set_image( GTK_IMAGE_MENU_ITEM( pMenuItem ), pImage );
186 : g_signal_connect_data( pMenuItem, "activate", pFnCallback, pURL,
187 0 : oustring_delete, GConnectFlags(0));
188 :
189 0 : gtk_menu_shell_append( pMenuShell, pMenuItem );
190 0 : }
191 :
192 : // Unbelievably nasty
193 : using namespace ::com::sun::star::uno;
194 : using namespace ::com::sun::star::task;
195 : using namespace ::com::sun::star::lang;
196 : using namespace ::com::sun::star::beans;
197 :
198 0 : static void add_ugly_db_item( GtkMenuShell *pMenuShell, const char *pAsciiURL,
199 : sal_uInt16 nResId, GCallback pFnCallback )
200 : {
201 0 : SvtDynamicMenuOptions aOpt;
202 0 : Sequence < Sequence < PropertyValue > > aMenu = aOpt.GetMenu( E_NEWMENU );
203 0 : for ( sal_Int32 n=0; n<aMenu.getLength(); n++ )
204 : {
205 0 : OUString aURL;
206 0 : OUString aDescription;
207 0 : Sequence < PropertyValue >& aEntry = aMenu[n];
208 0 : for ( sal_Int32 m=0; m<aEntry.getLength(); m++ )
209 : {
210 0 : if ( aEntry[m].Name == "URL" )
211 0 : aEntry[m].Value >>= aURL;
212 0 : if ( aEntry[m].Name == "Title" )
213 0 : aEntry[m].Value >>= aDescription;
214 : }
215 :
216 0 : if ( aURL == BASE_URL && !aDescription.isEmpty() )
217 : {
218 0 : add_item (pMenuShell, pAsciiURL, &aDescription, nResId, pFnCallback);
219 0 : break;
220 : }
221 0 : }
222 0 : }
223 :
224 : static GtkWidget *
225 0 : add_image_menu_item( GtkMenuShell *pMenuShell,
226 : const gchar *stock_id,
227 : const OUString& aLabel,
228 : GCallback activate_cb )
229 : {
230 0 : OString aUtfLabel = OUStringToOString (aLabel, RTL_TEXTENCODING_UTF8 );
231 :
232 : GtkWidget *pImage;
233 0 : pImage = gtk_image_new_from_stock( stock_id, GTK_ICON_SIZE_MENU );
234 :
235 : GtkWidget *pMenuItem;
236 0 : pMenuItem = gtk_image_menu_item_new_with_label( aUtfLabel.getStr() );
237 0 : gtk_image_menu_item_set_image( GTK_IMAGE_MENU_ITEM( pMenuItem ), pImage );
238 :
239 0 : gtk_menu_shell_append( pMenuShell, pMenuItem );
240 0 : g_signal_connect( pMenuItem, "activate", activate_cb, NULL);
241 :
242 0 : return pMenuItem;
243 : }
244 :
245 0 : static void populate_menu( GtkWidget *pMenu )
246 : {
247 0 : ShutdownIcon *pShutdownIcon = ShutdownIcon::getInstance();
248 0 : GtkMenuShell *pMenuShell = GTK_MENU_SHELL( pMenu );
249 0 : SvtModuleOptions aModuleOptions;
250 :
251 0 : if ( aModuleOptions.IsWriter() )
252 : add_item (pMenuShell, WRITER_URL, NULL,
253 0 : SV_ICON_ID_TEXT, G_CALLBACK( open_url_cb ));
254 :
255 0 : if ( aModuleOptions.IsCalc() )
256 : add_item (pMenuShell, CALC_URL, NULL,
257 0 : SV_ICON_ID_SPREADSHEET, G_CALLBACK( open_url_cb ));
258 :
259 0 : if ( aModuleOptions.IsImpress() )
260 : add_item (pMenuShell, IMPRESS_URL, NULL,
261 0 : SV_ICON_ID_PRESENTATION, G_CALLBACK( open_url_cb ));
262 :
263 0 : if ( aModuleOptions.IsDraw() )
264 : add_item (pMenuShell, DRAW_URL, NULL,
265 0 : SV_ICON_ID_DRAWING, G_CALLBACK( open_url_cb ));
266 :
267 0 : if ( aModuleOptions.IsDataBase() )
268 : add_ugly_db_item (pMenuShell, BASE_URL,
269 0 : SV_ICON_ID_DATABASE, G_CALLBACK( open_url_cb ));
270 :
271 0 : if ( aModuleOptions.IsMath() )
272 : add_item (pMenuShell, MATH_URL, NULL,
273 0 : SV_ICON_ID_FORMULA, G_CALLBACK( open_url_cb ));
274 :
275 0 : OUString aULabel = pShutdownIcon->GetResString( STR_QUICKSTART_FROMTEMPLATE );
276 : add_item (pMenuShell, "dummy", &aULabel,
277 0 : SV_ICON_ID_TEMPLATE, G_CALLBACK( open_template_cb ));
278 :
279 : GtkWidget *pMenuItem;
280 :
281 0 : pMenuItem = gtk_separator_menu_item_new();
282 0 : gtk_menu_shell_append( pMenuShell, pMenuItem );
283 :
284 : pOpenMenuItem = add_image_menu_item
285 : (pMenuShell, GTK_STOCK_OPEN,
286 : pShutdownIcon->GetResString( STR_QUICKSTART_FILEOPEN ),
287 0 : G_CALLBACK( open_file_cb ));
288 :
289 :
290 0 : pMenuItem = gtk_separator_menu_item_new();
291 0 : gtk_menu_shell_append( pMenuShell, pMenuItem );
292 :
293 : pDisableMenuItem = add_image_menu_item
294 : ( pMenuShell, GTK_STOCK_CLOSE,
295 : pShutdownIcon->GetResString( STR_QUICKSTART_PRELAUNCH_UNX ),
296 0 : G_CALLBACK( systray_disable_cb ) );
297 :
298 0 : pMenuItem = gtk_separator_menu_item_new();
299 0 : gtk_menu_shell_append( pMenuShell, pMenuItem );
300 :
301 : pExitMenuItem = add_image_menu_item
302 : ( pMenuShell, GTK_STOCK_QUIT,
303 : pShutdownIcon->GetResString( STR_QUICKSTART_EXIT ),
304 0 : G_CALLBACK( exit_quickstarter_cb ) );
305 :
306 0 : gtk_widget_show_all( pMenu );
307 0 : }
308 :
309 0 : static void refresh_menu( GtkWidget *pMenu )
310 : {
311 0 : if (!pExitMenuItem)
312 0 : populate_menu( pMenu );
313 :
314 0 : bool bModal = ShutdownIcon::bModalMode;
315 0 : gtk_widget_set_sensitive( pExitMenuItem, !bModal);
316 0 : gtk_widget_set_sensitive( pOpenMenuItem, !bModal);
317 0 : gtk_widget_set_sensitive( pDisableMenuItem, !bModal);
318 0 : }
319 :
320 0 : static gboolean display_menu_cb( GtkWidget *,
321 : GdkEventButton *event, GtkWidget *pMenu )
322 : {
323 0 : if (event->button == 2)
324 0 : return sal_False;
325 :
326 0 : refresh_menu( pMenu );
327 :
328 0 : gtk_menu_popup( GTK_MENU( pMenu ), NULL, NULL,
329 : gtk_status_icon_position_menu, pTrayIcon,
330 0 : 0, event->time );
331 :
332 0 : return sal_True;
333 : }
334 :
335 : #ifdef ENABLE_GIO
336 : /*
337 : * If the quickstarter is running, then LibreOffice is
338 : * upgraded, then the old quickstarter is still running, but is now unreliable
339 : * as the old install has been deleted. A fairly intractable problem but we
340 : * can avoid much of the pain if we turn off the quickstarter if we detect
341 : * that it has been physically deleted or overwritten
342 : */
343 0 : static void notify_file_changed(GFileMonitor * /*gfilemonitor*/, GFile * /*arg1*/,
344 : GFile * /*arg2*/, GFileMonitorEvent event_type, gpointer /*user_data*/)
345 : {
346 : //Shutdown the quick starter if anything has happened to make it unsafe
347 : //to remain running, e.g. rpm --erased and all libs deleted, or
348 : //rpm --upgrade and libs being overwritten
349 0 : switch (event_type)
350 : {
351 : case G_FILE_MONITOR_EVENT_DELETED:
352 : case G_FILE_MONITOR_EVENT_CREATED:
353 : case G_FILE_MONITOR_EVENT_PRE_UNMOUNT:
354 : case G_FILE_MONITOR_EVENT_UNMOUNTED:
355 0 : exit_quickstarter_cb(GTK_WIDGET(pTrayIcon));
356 0 : break;
357 : default:
358 0 : break;
359 : }
360 0 : }
361 : #endif
362 :
363 0 : void SAL_DLLPUBLIC_EXPORT plugin_init_sys_tray()
364 : {
365 0 : ::SolarMutexGuard aGuard;
366 :
367 0 : if( /* need gtk_status to resolve */
368 0 : (gtk_check_version( 2, 10, 0 ) != NULL) ||
369 : /* we need the vcl plugin and mainloop initialized */
370 0 : !g_type_from_name( "GdkDisplay" ) )
371 0 : return;
372 :
373 0 : OString aLabel;
374 0 : ShutdownIcon *pShutdownIcon = ShutdownIcon::getInstance();
375 :
376 0 : aLabel = OUStringToOString (
377 : pShutdownIcon->GetResString( STR_QUICKSTART_TIP ),
378 0 : RTL_TEXTENCODING_UTF8 );
379 :
380 0 : pVCLResMgr = ResMgr::CreateResMgr("vcl");
381 :
382 0 : GdkPixbuf *pPixbuf = ResIdToPixbuf( SV_ICON_SMALL_START + SV_ICON_ID_OFFICE );
383 0 : pTrayIcon = gtk_status_icon_new_from_pixbuf(pPixbuf);
384 0 : g_object_unref( pPixbuf );
385 :
386 : g_object_set (pTrayIcon, "title", aLabel.getStr(),
387 0 : "tooltip_text", aLabel.getStr(), NULL);
388 :
389 0 : GtkWidget *pMenu = gtk_menu_new();
390 0 : g_signal_connect(pTrayIcon, "button-press-event",
391 0 : G_CALLBACK(display_menu_cb), pMenu);
392 : g_signal_connect (pMenu, "deactivate",
393 0 : G_CALLBACK (menu_deactivate_cb), NULL);
394 :
395 : // disable shutdown
396 0 : pShutdownIcon->SetVeto( true );
397 0 : pShutdownIcon->addTerminateListener();
398 :
399 : #ifdef ENABLE_GIO
400 0 : GFile* pFile = NULL;
401 0 : OUString sLibraryFileUrl;
402 0 : if (osl::Module::getUrlFromAddress(plugin_init_sys_tray, sLibraryFileUrl))
403 0 : pFile = g_file_new_for_uri(OUStringToOString(sLibraryFileUrl, RTL_TEXTENCODING_UTF8).getStr());
404 :
405 0 : if (pFile)
406 : {
407 0 : if ((pMonitor = g_file_monitor_file(pFile, G_FILE_MONITOR_NONE, NULL, NULL)))
408 0 : g_signal_connect(pMonitor, "changed", (GCallback)notify_file_changed, NULL);
409 0 : g_object_unref(pFile);
410 0 : }
411 : #endif
412 : }
413 :
414 0 : void SAL_DLLPUBLIC_EXPORT plugin_shutdown_sys_tray()
415 : {
416 0 : ::SolarMutexGuard aGuard;
417 0 : if( !pTrayIcon )
418 0 : return;
419 :
420 : #ifdef ENABLE_GIO
421 0 : if (pMonitor)
422 : {
423 0 : g_signal_handlers_disconnect_by_func(pMonitor,
424 0 : (void*)notify_file_changed, pMonitor);
425 0 : g_file_monitor_cancel(pMonitor);
426 0 : g_object_unref(pMonitor);
427 0 : pMonitor = NULL;
428 : }
429 : #endif
430 :
431 0 : g_object_unref(pTrayIcon);
432 0 : pTrayIcon = NULL;
433 :
434 0 : pExitMenuItem = NULL;
435 0 : pOpenMenuItem = NULL;
436 0 : pDisableMenuItem = NULL;
437 : }
438 :
439 : #endif // ENABLE_QUICKSTART_APPLET
440 :
441 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|