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