Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / sfx2 / source / appl / shutdowniconunx.cxx
blobd4062c346486e6eb847b326ecb374ed83e10986f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 #ifdef ENABLE_QUICKSTART_APPLET
5 #include <unotools/moduleoptions.hxx>
7 #include <unotools/dynamicmenuoptions.hxx>
9 #include <gtk/gtk.h>
10 #include <glib.h>
11 #include <osl/mutex.hxx>
12 #include <vcl/bitmapex.hxx>
13 #include <vcl/bmpacc.hxx>
14 #include <sfx2/app.hxx>
15 #include "app.hrc"
16 #ifndef __SHUTDOWNICON_HXX__
17 #define USE_APP_SHORTCUTS
18 #include "shutdownicon.hxx"
19 #endif
21 #ifdef ENABLE_GIO
22 #include <gio/gio.h>
23 #endif
25 // Cut/paste from vcl/inc/svids.hrc
26 #define SV_ICON_SMALL_START 25000
28 #define SV_ICON_ID_OFFICE 1
29 #define SV_ICON_ID_TEXT 2
30 #define SV_ICON_ID_SPREADSHEET 4
31 #define SV_ICON_ID_DRAWING 6
32 #define SV_ICON_ID_PRESENTATION 8
33 #define SV_ICON_ID_TEMPLATE 11
34 #define SV_ICON_ID_DATABASE 12
35 #define SV_ICON_ID_FORMULA 13
37 using namespace ::rtl;
38 using namespace ::osl;
40 static ResMgr *pVCLResMgr;
41 static GtkStatusIcon* pTrayIcon;
42 static GtkWidget *pExitMenuItem = NULL;
43 static GtkWidget *pOpenMenuItem = NULL;
44 static GtkWidget *pDisableMenuItem = NULL;
45 #ifdef ENABLE_GIO
46 GFileMonitor* pMonitor = NULL;
47 #endif
49 static void open_url_cb( GtkWidget *, gpointer data )
51 ShutdownIcon::OpenURL( *(OUString *)data,
52 OUString( RTL_CONSTASCII_USTRINGPARAM( "_default" ) ) );
55 static void open_file_cb( GtkWidget * )
57 if ( !ShutdownIcon::bModalMode )
58 ShutdownIcon::FileOpen();
61 static void open_template_cb( GtkWidget * )
63 if ( !ShutdownIcon::bModalMode )
64 ShutdownIcon::FromTemplate();
67 static void systray_disable_cb()
69 ShutdownIcon::SetAutostart( false );
70 ShutdownIcon::terminateDesktop();
73 static void exit_quickstarter_cb( GtkWidget * )
75 plugin_shutdown_sys_tray();
76 //terminate may cause this .so to be unloaded. So we must be hands off
77 //all calls into this .so after this call
78 ShutdownIcon::terminateDesktop();
81 static void menu_deactivate_cb( GtkWidget *pMenu )
83 gtk_menu_popdown( GTK_MENU( pMenu ) );
86 static GdkPixbuf * ResIdToPixbuf( sal_uInt16 nResId )
88 ResId aResId( SV_ICON_SMALL_START + nResId, *pVCLResMgr );
89 BitmapEx aIcon( aResId );
90 Bitmap pInSalBitmap = aIcon.GetBitmap();
91 AlphaMask pInSalAlpha = aIcon.GetAlpha();
93 if( pInSalBitmap.GetBitCount() != 24 )
94 pInSalBitmap.Convert( BMP_CONVERSION_24BIT );
96 Bitmap::ScopedReadAccess pSalBitmap(pInSalBitmap);
97 AlphaMask::ScopedReadAccess pSalAlpha(pInSalAlpha);
99 g_return_val_if_fail( pSalBitmap, NULL );
101 Size aSize( pSalBitmap->Width(), pSalBitmap->Height() );
102 if (pSalAlpha)
103 g_return_val_if_fail( Size( pSalAlpha->Width(), pSalAlpha->Height() ) == aSize, NULL );
105 int nX, nY;
106 guchar *pPixbufData = ( guchar * )g_malloc( 4 * aSize.Width() * aSize.Height() );
107 guchar *pDestData = pPixbufData;
109 for( nY = 0; nY < pSalBitmap->Height(); nY++ )
111 for( nX = 0; nX < pSalBitmap->Width(); nX++ )
113 BitmapColor aPix;
114 aPix = pSalBitmap->GetPixel( nY, nX );
115 pDestData[0] = aPix.GetRed();
116 pDestData[1] = aPix.GetGreen();
117 pDestData[2] = aPix.GetBlue();
118 if (pSalAlpha)
120 aPix = pSalAlpha->GetPixel( nY, nX );
121 pDestData[3] = 255 - aPix.GetIndex();
123 else
124 pDestData[3] = 255;
125 pDestData += 4;
129 return gdk_pixbuf_new_from_data( pPixbufData,
130 GDK_COLORSPACE_RGB, sal_True, 8,
131 aSize.Width(), aSize.Height(),
132 aSize.Width() * 4,
133 (GdkPixbufDestroyNotify) g_free,
134 NULL );
137 extern "C" {
138 static void oustring_delete (gpointer data,
139 GClosure * /* closure */)
141 OUString *pURL = (OUString *) data;
142 delete pURL;
146 static void add_item( GtkMenuShell *pMenuShell, const char *pAsciiURL,
147 OUString *pOverrideLabel,
148 sal_uInt16 nResId, GCallback pFnCallback )
150 OUString *pURL = new OUString (OStringToOUString( pAsciiURL,
151 RTL_TEXTENCODING_UTF8 ));
152 OString aLabel;
153 if (pOverrideLabel)
154 aLabel = OUStringToOString (*pOverrideLabel, RTL_TEXTENCODING_UTF8);
155 else
157 ShutdownIcon *pShutdownIcon = ShutdownIcon::getInstance();
158 aLabel = OUStringToOString (pShutdownIcon->GetUrlDescription( *pURL ),
159 RTL_TEXTENCODING_UTF8);
162 GdkPixbuf *pPixbuf= ResIdToPixbuf( nResId );
163 GtkWidget *pImage = gtk_image_new_from_pixbuf( pPixbuf );
164 g_object_unref( G_OBJECT( pPixbuf ) );
166 GtkWidget *pMenuItem = gtk_image_menu_item_new_with_label( aLabel.getStr() );
167 gtk_image_menu_item_set_image( GTK_IMAGE_MENU_ITEM( pMenuItem ), pImage );
168 g_signal_connect_data( pMenuItem, "activate", pFnCallback, pURL,
169 oustring_delete, GConnectFlags(0));
171 gtk_menu_shell_append( pMenuShell, pMenuItem );
174 // Unbelievably nasty
175 using namespace ::com::sun::star::uno;
176 using namespace ::com::sun::star::task;
177 using namespace ::com::sun::star::lang;
178 using namespace ::com::sun::star::beans;
180 static void add_ugly_db_item( GtkMenuShell *pMenuShell, const char *pAsciiURL,
181 sal_uInt16 nResId, GCallback pFnCallback )
183 SvtDynamicMenuOptions aOpt;
184 Sequence < Sequence < PropertyValue > > aMenu = aOpt.GetMenu( E_NEWMENU );
185 for ( sal_Int32 n=0; n<aMenu.getLength(); n++ )
187 ::rtl::OUString aURL;
188 ::rtl::OUString aDescription;
189 Sequence < PropertyValue >& aEntry = aMenu[n];
190 for ( sal_Int32 m=0; m<aEntry.getLength(); m++ )
192 if ( aEntry[m].Name == "URL" )
193 aEntry[m].Value >>= aURL;
194 if ( aEntry[m].Name == "Title" )
195 aEntry[m].Value >>= aDescription;
198 if ( aURL.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM(BASE_URL)) && !aDescription.isEmpty() )
200 add_item (pMenuShell, pAsciiURL, &aDescription, nResId, pFnCallback);
201 break;
206 static GtkWidget *
207 add_image_menu_item( GtkMenuShell *pMenuShell,
208 const gchar *stock_id,
209 rtl::OUString aLabel,
210 GCallback activate_cb )
212 OString aUtfLabel = rtl::OUStringToOString (aLabel, RTL_TEXTENCODING_UTF8 );
214 GtkWidget *pImage;
215 pImage = gtk_image_new_from_stock( stock_id, GTK_ICON_SIZE_MENU );
217 GtkWidget *pMenuItem;
218 pMenuItem = gtk_image_menu_item_new_with_label( aUtfLabel.getStr() );
219 gtk_image_menu_item_set_image( GTK_IMAGE_MENU_ITEM( pMenuItem ), pImage );
221 gtk_menu_shell_append( pMenuShell, pMenuItem );
222 g_signal_connect( pMenuItem, "activate", activate_cb, NULL);
224 return pMenuItem;
227 static void populate_menu( GtkWidget *pMenu )
229 ShutdownIcon *pShutdownIcon = ShutdownIcon::getInstance();
230 GtkMenuShell *pMenuShell = GTK_MENU_SHELL( pMenu );
231 SvtModuleOptions aModuleOptions;
233 if ( aModuleOptions.IsWriter() )
234 add_item (pMenuShell, WRITER_URL, NULL,
235 SV_ICON_ID_TEXT, G_CALLBACK( open_url_cb ));
237 if ( aModuleOptions.IsCalc() )
238 add_item (pMenuShell, CALC_URL, NULL,
239 SV_ICON_ID_SPREADSHEET, G_CALLBACK( open_url_cb ));
241 if ( aModuleOptions.IsImpress() )
242 add_item (pMenuShell, IMPRESS_URL, NULL,
243 SV_ICON_ID_PRESENTATION, G_CALLBACK( open_url_cb ));
245 if ( aModuleOptions.IsDraw() )
246 add_item (pMenuShell, DRAW_URL, NULL,
247 SV_ICON_ID_DRAWING, G_CALLBACK( open_url_cb ));
249 if ( aModuleOptions.IsDataBase() )
250 add_ugly_db_item (pMenuShell, BASE_URL,
251 SV_ICON_ID_DATABASE, G_CALLBACK( open_url_cb ));
253 if ( aModuleOptions.IsMath() )
254 add_item (pMenuShell, MATH_URL, NULL,
255 SV_ICON_ID_FORMULA, G_CALLBACK( open_url_cb ));
257 OUString aULabel = pShutdownIcon->GetResString( STR_QUICKSTART_FROMTEMPLATE );
258 add_item (pMenuShell, "dummy", &aULabel,
259 SV_ICON_ID_TEMPLATE, G_CALLBACK( open_template_cb ));
261 OString aLabel;
262 GtkWidget *pMenuItem;
264 pMenuItem = gtk_separator_menu_item_new();
265 gtk_menu_shell_append( pMenuShell, pMenuItem );
267 pOpenMenuItem = add_image_menu_item
268 (pMenuShell, GTK_STOCK_OPEN,
269 pShutdownIcon->GetResString( STR_QUICKSTART_FILEOPEN ),
270 G_CALLBACK( open_file_cb ));
272 pMenuItem = gtk_separator_menu_item_new();
273 gtk_menu_shell_append( pMenuShell, pMenuItem );
275 pDisableMenuItem = add_image_menu_item
276 ( pMenuShell, GTK_STOCK_CLOSE,
277 pShutdownIcon->GetResString( STR_QUICKSTART_PRELAUNCH_UNX ),
278 G_CALLBACK( systray_disable_cb ) );
280 pMenuItem = gtk_separator_menu_item_new();
281 gtk_menu_shell_append( pMenuShell, pMenuItem );
283 pExitMenuItem = add_image_menu_item
284 ( pMenuShell, GTK_STOCK_QUIT,
285 pShutdownIcon->GetResString( STR_QUICKSTART_EXIT ),
286 G_CALLBACK( exit_quickstarter_cb ) );
288 gtk_widget_show_all( pMenu );
291 static void refresh_menu( GtkWidget *pMenu )
293 if (!pExitMenuItem)
294 populate_menu( pMenu );
296 bool bModal = ShutdownIcon::bModalMode;
297 gtk_widget_set_sensitive( pExitMenuItem, !bModal);
298 gtk_widget_set_sensitive( pOpenMenuItem, !bModal);
299 gtk_widget_set_sensitive( pDisableMenuItem, !bModal);
302 static gboolean display_menu_cb( GtkWidget *,
303 GdkEventButton *event, GtkWidget *pMenu )
305 if (event->button == 2)
306 return sal_False;
308 refresh_menu( pMenu );
310 gtk_menu_popup( GTK_MENU( pMenu ), NULL, NULL,
311 gtk_status_icon_position_menu, pTrayIcon,
312 0, event->time );
314 return sal_True;
317 #ifdef ENABLE_GIO
319 * If the quickstarter is running, then LibreOffice is
320 * upgraded, then the old quickstarter is still running, but is now unreliable
321 * as the old install has been deleted. A fairly intractable problem but we
322 * can avoid much of the pain if we turn off the quickstarter if we detect
323 * that it has been physically deleted or overwritten
325 static void notify_file_changed(GFileMonitor * /*gfilemonitor*/, GFile * /*arg1*/,
326 GFile * /*arg2*/, GFileMonitorEvent event_type, gpointer /*user_data*/)
328 //Shutdown the quick starter if anything has happened to make it unsafe
329 //to remain running, e.g. rpm --erased and all libs deleted, or
330 //rpm --upgrade and libs being overwritten
331 switch (event_type)
333 case G_FILE_MONITOR_EVENT_DELETED:
334 case G_FILE_MONITOR_EVENT_CREATED:
335 case G_FILE_MONITOR_EVENT_PRE_UNMOUNT:
336 case G_FILE_MONITOR_EVENT_UNMOUNTED:
337 exit_quickstarter_cb(GTK_WIDGET(pTrayIcon));
338 break;
339 default:
340 break;
343 #endif
345 void SAL_DLLPUBLIC_EXPORT plugin_init_sys_tray()
347 ::SolarMutexGuard aGuard;
349 if( /* need gtk_status to resolve */
350 (gtk_check_version( 2, 10, 0 ) != NULL) ||
351 /* we need the vcl plugin and mainloop initialized */
352 !g_type_from_name( "GdkDisplay" ) )
353 return;
355 OString aLabel;
356 ShutdownIcon *pShutdownIcon = ShutdownIcon::getInstance();
358 aLabel = rtl::OUStringToOString (
359 pShutdownIcon->GetResString( STR_QUICKSTART_TIP ),
360 RTL_TEXTENCODING_UTF8 );
362 pVCLResMgr = ResMgr::CreateResMgr("vcl");
364 GdkPixbuf *pPixbuf = ResIdToPixbuf( SV_ICON_ID_OFFICE );
365 pTrayIcon = gtk_status_icon_new_from_pixbuf(pPixbuf);
366 g_object_unref( pPixbuf );
368 g_object_set (pTrayIcon, "title", aLabel.getStr(),
369 "tooltip_text", aLabel.getStr(), NULL);
371 GtkWidget *pMenu = gtk_menu_new();
372 g_signal_connect (pMenu, "deactivate",
373 G_CALLBACK (menu_deactivate_cb), NULL);
374 g_signal_connect(pTrayIcon, "button_press_event",
375 G_CALLBACK(display_menu_cb), pMenu);
377 // disable shutdown
378 pShutdownIcon->SetVeto( true );
379 pShutdownIcon->addTerminateListener();
381 #ifdef ENABLE_GIO
382 GFile* pFile = NULL;
383 rtl::OUString sLibraryFileUrl;
384 if (osl::Module::getUrlFromAddress(plugin_init_sys_tray, sLibraryFileUrl))
385 pFile = g_file_new_for_uri(rtl::OUStringToOString(sLibraryFileUrl, RTL_TEXTENCODING_UTF8).getStr());
387 if (pFile)
389 if ((pMonitor = g_file_monitor_file(pFile, G_FILE_MONITOR_NONE, NULL, NULL)))
390 g_signal_connect(pMonitor, "changed", (GCallback)notify_file_changed, NULL);
391 g_object_unref(pFile);
393 #endif
396 void SAL_DLLPUBLIC_EXPORT plugin_shutdown_sys_tray()
398 ::SolarMutexGuard aGuard;
399 if( !pTrayIcon )
400 return;
402 #ifdef ENABLE_GIO
403 if (pMonitor)
405 g_signal_handlers_disconnect_by_func(pMonitor,
406 (void*)notify_file_changed, pMonitor);
407 g_file_monitor_cancel(pMonitor);
408 g_object_unref(pMonitor);
409 pMonitor = NULL;
411 #endif
413 g_object_unref(pTrayIcon);
414 pTrayIcon = NULL;
416 pExitMenuItem = NULL;
417 pOpenMenuItem = NULL;
418 pDisableMenuItem = NULL;
421 #endif // ENABLE_QUICKSTART_APPLET
423 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */