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