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 <sfx2/app.hxx>
32 : #include "app.hrc"
33 : #ifndef __SHUTDOWNICON_HXX__
34 : #define USE_APP_SHORTCUTS
35 : #include "shutdownicon.hxx"
36 : #endif
37 :
38 : #ifdef ENABLE_GIO
39 : #include <gio/gio.h>
40 : #endif
41 :
42 : // Cut/paste from vcl/inc/svids.hrc
43 : #define SV_ICON_SMALL_START 25000
44 :
45 : #define SV_ICON_ID_OFFICE 1
46 : #define SV_ICON_ID_TEXT 2
47 : #define SV_ICON_ID_SPREADSHEET 4
48 : #define SV_ICON_ID_DRAWING 6
49 : #define SV_ICON_ID_PRESENTATION 8
50 : #define SV_ICON_ID_TEMPLATE 11
51 : #define SV_ICON_ID_DATABASE 12
52 : #define SV_ICON_ID_FORMULA 13
53 :
54 : using namespace ::rtl;
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( RTL_CONSTASCII_USTRINGPARAM( "_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 : 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 : ::rtl::OUString aURL;
205 0 : ::rtl::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.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM(BASE_URL)) && !aDescription.isEmpty() )
216 : {
217 0 : add_item (pMenuShell, pAsciiURL, &aDescription, nResId, pFnCallback);
218 : break;
219 : }
220 0 : }
221 0 : }
222 :
223 : static GtkWidget *
224 0 : add_image_menu_item( GtkMenuShell *pMenuShell,
225 : const gchar *stock_id,
226 : rtl::OUString aLabel,
227 : GCallback activate_cb )
228 : {
229 0 : OString aUtfLabel = rtl::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 0 : OString aLabel;
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 : 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 : 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 : exit_quickstarter_cb(GTK_WIDGET(pTrayIcon));
356 : break;
357 : default:
358 : break;
359 : }
360 : }
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 : aLabel = rtl::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 : GFile* pFile = NULL;
401 : rtl::OUString sLibraryFileUrl;
402 : if (osl::Module::getUrlFromAddress(plugin_init_sys_tray, sLibraryFileUrl))
403 : pFile = g_file_new_for_uri(rtl::OUStringToOString(sLibraryFileUrl, RTL_TEXTENCODING_UTF8).getStr());
404 :
405 : if (pFile)
406 : {
407 : if ((pMonitor = g_file_monitor_file(pFile, G_FILE_MONITOR_NONE, NULL, NULL)))
408 : g_signal_connect(pMonitor, "changed", (GCallback)notify_file_changed, NULL);
409 : g_object_unref(pFile);
410 : }
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 : if (pMonitor)
422 : {
423 : g_signal_handlers_disconnect_by_func(pMonitor,
424 : (void*)notify_file_changed, pMonitor);
425 : g_file_monitor_cancel(pMonitor);
426 : g_object_unref(pMonitor);
427 : 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: */
|