bump product version to 4.2.0.1
[LibreOffice.git] / sfx2 / source / appl / shutdowniconunx.cxx
blob937cabe8de6dd7af2a0ec11244da44ac7395d408
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifdef ENABLE_QUICKSTART_APPLET
22 #include <unotools/moduleoptions.hxx>
24 #include <unotools/dynamicmenuoptions.hxx>
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
38 #ifdef ENABLE_GIO
39 #include <gio/gio.h>
40 #endif
42 // Cut/paste from vcl/inc/svids.hrc
43 #define SV_ICON_SMALL_START 25000
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
54 using namespace ::rtl;
55 using namespace ::osl;
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
66 static void open_url_cb( GtkWidget *, gpointer data )
68 ShutdownIcon::OpenURL( *(OUString *)data,
69 OUString( "_default" ) );
72 static void open_file_cb( GtkWidget * )
74 if ( !ShutdownIcon::bModalMode )
75 ShutdownIcon::FileOpen();
78 static void open_template_cb( GtkWidget * )
80 if ( !ShutdownIcon::bModalMode )
81 ShutdownIcon::FromTemplate();
84 static void systray_disable_cb()
86 ShutdownIcon::SetAutostart( false );
87 ShutdownIcon::terminateDesktop();
90 static void exit_quickstarter_cb( GtkWidget * )
92 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 ShutdownIcon::terminateDesktop();
98 static void menu_deactivate_cb( GtkWidget *pMenu )
100 gtk_menu_popdown( GTK_MENU( pMenu ) );
103 static GdkPixbuf * ResIdToPixbuf( sal_uInt16 nResId )
105 ResId aResId( nResId, *pVCLResMgr );
106 BitmapEx aIcon( aResId );
107 Bitmap pInSalBitmap = aIcon.GetBitmap();
108 AlphaMask pInSalAlpha = aIcon.GetAlpha();
110 if( pInSalBitmap.GetBitCount() != 24 )
111 pInSalBitmap.Convert( BMP_CONVERSION_24BIT );
113 Bitmap::ScopedReadAccess pSalBitmap(pInSalBitmap);
114 AlphaMask::ScopedReadAccess pSalAlpha(pInSalAlpha);
116 g_return_val_if_fail( pSalBitmap, NULL );
118 Size aSize( pSalBitmap->Width(), pSalBitmap->Height() );
119 if (pSalAlpha)
120 g_return_val_if_fail( Size( pSalAlpha->Width(), pSalAlpha->Height() ) == aSize, NULL );
122 int nX, nY;
123 guchar *pPixbufData = ( guchar * )g_malloc( 4 * aSize.Width() * aSize.Height() );
124 guchar *pDestData = pPixbufData;
126 for( nY = 0; nY < pSalBitmap->Height(); nY++ )
128 for( nX = 0; nX < pSalBitmap->Width(); nX++ )
130 BitmapColor aPix;
131 aPix = pSalBitmap->GetPixel( nY, nX );
132 pDestData[0] = aPix.GetRed();
133 pDestData[1] = aPix.GetGreen();
134 pDestData[2] = aPix.GetBlue();
135 if (pSalAlpha)
137 aPix = pSalAlpha->GetPixel( nY, nX );
138 pDestData[3] = 255 - aPix.GetIndex();
140 else
141 pDestData[3] = 255;
142 pDestData += 4;
146 return gdk_pixbuf_new_from_data( pPixbufData,
147 GDK_COLORSPACE_RGB, sal_True, 8,
148 aSize.Width(), aSize.Height(),
149 aSize.Width() * 4,
150 (GdkPixbufDestroyNotify) g_free,
151 NULL );
154 extern "C" {
155 static void oustring_delete (gpointer data,
156 GClosure * /* closure */)
158 OUString *pURL = (OUString *) data;
159 delete pURL;
163 static void add_item( GtkMenuShell *pMenuShell, const char *pAsciiURL,
164 OUString *pOverrideLabel,
165 sal_uInt16 nResId, GCallback pFnCallback )
167 OUString *pURL = new OUString (OStringToOUString( pAsciiURL,
168 RTL_TEXTENCODING_UTF8 ));
169 OString aLabel;
170 if (pOverrideLabel)
171 aLabel = OUStringToOString (*pOverrideLabel, RTL_TEXTENCODING_UTF8);
172 else
174 ShutdownIcon *pShutdownIcon = ShutdownIcon::getInstance();
175 aLabel = OUStringToOString (pShutdownIcon->GetUrlDescription( *pURL ),
176 RTL_TEXTENCODING_UTF8);
179 GdkPixbuf *pPixbuf = ResIdToPixbuf( SV_ICON_SMALL_START + nResId );
180 GtkWidget *pImage = gtk_image_new_from_pixbuf( pPixbuf );
181 g_object_unref( G_OBJECT( pPixbuf ) );
183 GtkWidget *pMenuItem = gtk_image_menu_item_new_with_label( aLabel.getStr() );
184 gtk_image_menu_item_set_image( GTK_IMAGE_MENU_ITEM( pMenuItem ), pImage );
185 g_signal_connect_data( pMenuItem, "activate", pFnCallback, pURL,
186 oustring_delete, GConnectFlags(0));
188 gtk_menu_shell_append( pMenuShell, pMenuItem );
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;
197 static void add_ugly_db_item( GtkMenuShell *pMenuShell, const char *pAsciiURL,
198 sal_uInt16 nResId, GCallback pFnCallback )
200 SvtDynamicMenuOptions aOpt;
201 Sequence < Sequence < PropertyValue > > aMenu = aOpt.GetMenu( E_NEWMENU );
202 for ( sal_Int32 n=0; n<aMenu.getLength(); n++ )
204 OUString aURL;
205 OUString aDescription;
206 Sequence < PropertyValue >& aEntry = aMenu[n];
207 for ( sal_Int32 m=0; m<aEntry.getLength(); m++ )
209 if ( aEntry[m].Name == "URL" )
210 aEntry[m].Value >>= aURL;
211 if ( aEntry[m].Name == "Title" )
212 aEntry[m].Value >>= aDescription;
215 if ( aURL.startsWith(BASE_URL) && !aDescription.isEmpty() )
217 add_item (pMenuShell, pAsciiURL, &aDescription, nResId, pFnCallback);
218 break;
223 static GtkWidget *
224 add_image_menu_item( GtkMenuShell *pMenuShell,
225 const gchar *stock_id,
226 OUString aLabel,
227 GCallback activate_cb )
229 OString aUtfLabel = OUStringToOString (aLabel, RTL_TEXTENCODING_UTF8 );
231 GtkWidget *pImage;
232 pImage = gtk_image_new_from_stock( stock_id, GTK_ICON_SIZE_MENU );
234 GtkWidget *pMenuItem;
235 pMenuItem = gtk_image_menu_item_new_with_label( aUtfLabel.getStr() );
236 gtk_image_menu_item_set_image( GTK_IMAGE_MENU_ITEM( pMenuItem ), pImage );
238 gtk_menu_shell_append( pMenuShell, pMenuItem );
239 g_signal_connect( pMenuItem, "activate", activate_cb, NULL);
241 return pMenuItem;
244 static void populate_menu( GtkWidget *pMenu )
246 ShutdownIcon *pShutdownIcon = ShutdownIcon::getInstance();
247 GtkMenuShell *pMenuShell = GTK_MENU_SHELL( pMenu );
248 SvtModuleOptions aModuleOptions;
250 if ( aModuleOptions.IsWriter() )
251 add_item (pMenuShell, WRITER_URL, NULL,
252 SV_ICON_ID_TEXT, G_CALLBACK( open_url_cb ));
254 if ( aModuleOptions.IsCalc() )
255 add_item (pMenuShell, CALC_URL, NULL,
256 SV_ICON_ID_SPREADSHEET, G_CALLBACK( open_url_cb ));
258 if ( aModuleOptions.IsImpress() )
259 add_item (pMenuShell, IMPRESS_URL, NULL,
260 SV_ICON_ID_PRESENTATION, G_CALLBACK( open_url_cb ));
262 if ( aModuleOptions.IsDraw() )
263 add_item (pMenuShell, DRAW_URL, NULL,
264 SV_ICON_ID_DRAWING, G_CALLBACK( open_url_cb ));
266 if ( aModuleOptions.IsDataBase() )
267 add_ugly_db_item (pMenuShell, BASE_URL,
268 SV_ICON_ID_DATABASE, G_CALLBACK( open_url_cb ));
270 if ( aModuleOptions.IsMath() )
271 add_item (pMenuShell, MATH_URL, NULL,
272 SV_ICON_ID_FORMULA, G_CALLBACK( open_url_cb ));
274 OUString aULabel = pShutdownIcon->GetResString( STR_QUICKSTART_FROMTEMPLATE );
275 add_item (pMenuShell, "dummy", &aULabel,
276 SV_ICON_ID_TEMPLATE, G_CALLBACK( open_template_cb ));
278 GtkWidget *pMenuItem;
280 pMenuItem = gtk_separator_menu_item_new();
281 gtk_menu_shell_append( pMenuShell, pMenuItem );
283 pOpenMenuItem = add_image_menu_item
284 (pMenuShell, GTK_STOCK_OPEN,
285 pShutdownIcon->GetResString( STR_QUICKSTART_FILEOPEN ),
286 G_CALLBACK( open_file_cb ));
289 pMenuItem = gtk_separator_menu_item_new();
290 gtk_menu_shell_append( pMenuShell, pMenuItem );
292 pDisableMenuItem = add_image_menu_item
293 ( pMenuShell, GTK_STOCK_CLOSE,
294 pShutdownIcon->GetResString( STR_QUICKSTART_PRELAUNCH_UNX ),
295 G_CALLBACK( systray_disable_cb ) );
297 pMenuItem = gtk_separator_menu_item_new();
298 gtk_menu_shell_append( pMenuShell, pMenuItem );
300 pExitMenuItem = add_image_menu_item
301 ( pMenuShell, GTK_STOCK_QUIT,
302 pShutdownIcon->GetResString( STR_QUICKSTART_EXIT ),
303 G_CALLBACK( exit_quickstarter_cb ) );
305 gtk_widget_show_all( pMenu );
308 static void refresh_menu( GtkWidget *pMenu )
310 if (!pExitMenuItem)
311 populate_menu( pMenu );
313 bool bModal = ShutdownIcon::bModalMode;
314 gtk_widget_set_sensitive( pExitMenuItem, !bModal);
315 gtk_widget_set_sensitive( pOpenMenuItem, !bModal);
316 gtk_widget_set_sensitive( pDisableMenuItem, !bModal);
319 static gboolean display_menu_cb( GtkWidget *,
320 GdkEventButton *event, GtkWidget *pMenu )
322 if (event->button == 2)
323 return sal_False;
325 refresh_menu( pMenu );
327 gtk_menu_popup( GTK_MENU( pMenu ), NULL, NULL,
328 gtk_status_icon_position_menu, pTrayIcon,
329 0, event->time );
331 return sal_True;
334 #ifdef ENABLE_GIO
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
342 static void notify_file_changed(GFileMonitor * /*gfilemonitor*/, GFile * /*arg1*/,
343 GFile * /*arg2*/, GFileMonitorEvent event_type, gpointer /*user_data*/)
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 switch (event_type)
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 exit_quickstarter_cb(GTK_WIDGET(pTrayIcon));
355 break;
356 default:
357 break;
360 #endif
362 void SAL_DLLPUBLIC_EXPORT plugin_init_sys_tray()
364 ::SolarMutexGuard aGuard;
366 if( /* need gtk_status to resolve */
367 (gtk_check_version( 2, 10, 0 ) != NULL) ||
368 /* we need the vcl plugin and mainloop initialized */
369 !g_type_from_name( "GdkDisplay" ) )
370 return;
372 OString aLabel;
373 ShutdownIcon *pShutdownIcon = ShutdownIcon::getInstance();
375 aLabel = OUStringToOString (
376 pShutdownIcon->GetResString( STR_QUICKSTART_TIP ),
377 RTL_TEXTENCODING_UTF8 );
379 pVCLResMgr = ResMgr::CreateResMgr("vcl");
381 GdkPixbuf *pPixbuf = ResIdToPixbuf( SV_ICON_SMALL_START + SV_ICON_ID_OFFICE );
382 pTrayIcon = gtk_status_icon_new_from_pixbuf(pPixbuf);
383 g_object_unref( pPixbuf );
385 g_object_set (pTrayIcon, "title", aLabel.getStr(),
386 "tooltip_text", aLabel.getStr(), NULL);
388 GtkWidget *pMenu = gtk_menu_new();
389 g_signal_connect(pTrayIcon, "button-press-event",
390 G_CALLBACK(display_menu_cb), pMenu);
391 g_signal_connect (pMenu, "deactivate",
392 G_CALLBACK (menu_deactivate_cb), NULL);
394 // disable shutdown
395 pShutdownIcon->SetVeto( true );
396 pShutdownIcon->addTerminateListener();
398 #ifdef ENABLE_GIO
399 GFile* pFile = NULL;
400 OUString sLibraryFileUrl;
401 if (osl::Module::getUrlFromAddress(plugin_init_sys_tray, sLibraryFileUrl))
402 pFile = g_file_new_for_uri(OUStringToOString(sLibraryFileUrl, RTL_TEXTENCODING_UTF8).getStr());
404 if (pFile)
406 if ((pMonitor = g_file_monitor_file(pFile, G_FILE_MONITOR_NONE, NULL, NULL)))
407 g_signal_connect(pMonitor, "changed", (GCallback)notify_file_changed, NULL);
408 g_object_unref(pFile);
410 #endif
413 void SAL_DLLPUBLIC_EXPORT plugin_shutdown_sys_tray()
415 ::SolarMutexGuard aGuard;
416 if( !pTrayIcon )
417 return;
419 #ifdef ENABLE_GIO
420 if (pMonitor)
422 g_signal_handlers_disconnect_by_func(pMonitor,
423 (void*)notify_file_changed, pMonitor);
424 g_file_monitor_cancel(pMonitor);
425 g_object_unref(pMonitor);
426 pMonitor = NULL;
428 #endif
430 g_object_unref(pTrayIcon);
431 pTrayIcon = NULL;
433 pExitMenuItem = NULL;
434 pOpenMenuItem = NULL;
435 pDisableMenuItem = NULL;
438 #endif // ENABLE_QUICKSTART_APPLET
440 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */