1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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>
35 #ifndef __SHUTDOWNICON_HXX__
36 #define USE_APP_SHORTCUTS
37 #include "shutdownicon.hxx"
44 // Cut/paste from vcl/inc/svids.hrc
45 #define SV_ICON_SMALL_START 25000
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
56 using namespace ::osl
;
60 void SAL_DLLPUBLIC_EXPORT
plugin_init_sys_tray();
61 void SAL_DLLPUBLIC_EXPORT
plugin_shutdown_sys_tray();
65 static ResMgr
*pVCLResMgr
;
66 static GtkStatusIcon
* pTrayIcon
;
67 static GtkWidget
*pExitMenuItem
= NULL
;
68 static GtkWidget
*pOpenMenuItem
= NULL
;
69 static GtkWidget
*pDisableMenuItem
= NULL
;
71 GFileMonitor
* pMonitor
= NULL
;
74 static void open_url_cb( GtkWidget
*, gpointer data
)
76 ShutdownIcon::OpenURL( *static_cast<OUString
*>(data
),
77 OUString( "_default" ) );
80 static void open_file_cb( GtkWidget
* )
82 if ( !ShutdownIcon::bModalMode
)
83 ShutdownIcon::FileOpen();
86 static void open_template_cb( GtkWidget
* )
88 if ( !ShutdownIcon::bModalMode
)
89 ShutdownIcon::FromTemplate();
92 static void systray_disable_cb()
94 ShutdownIcon::SetAutostart( false );
95 ShutdownIcon::terminateDesktop();
98 static void exit_quickstarter_cb( GtkWidget
* )
100 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 ShutdownIcon::terminateDesktop();
106 static void menu_deactivate_cb( GtkWidget
*pMenu
)
108 gtk_menu_popdown( GTK_MENU( pMenu
) );
111 static GdkPixbuf
* ResIdToPixbuf( sal_uInt16 nResId
)
113 ResId
aResId( nResId
, *pVCLResMgr
);
114 BitmapEx
aIcon( aResId
);
115 Bitmap pInSalBitmap
= aIcon
.GetBitmap();
116 AlphaMask pInSalAlpha
= aIcon
.GetAlpha();
118 if( pInSalBitmap
.GetBitCount() != 24 )
119 pInSalBitmap
.Convert( BMP_CONVERSION_24BIT
);
121 Bitmap::ScopedReadAccess
pSalBitmap(pInSalBitmap
);
122 AlphaMask::ScopedReadAccess
pSalAlpha(pInSalAlpha
);
124 g_return_val_if_fail( pSalBitmap
, NULL
);
126 Size
aSize( pSalBitmap
->Width(), pSalBitmap
->Height() );
128 g_return_val_if_fail( Size( pSalAlpha
->Width(), pSalAlpha
->Height() ) == aSize
, NULL
);
131 guchar
*pPixbufData
= static_cast<guchar
*>(g_malloc( 4 * aSize
.Width() * aSize
.Height() ));
132 guchar
*pDestData
= pPixbufData
;
134 for( nY
= 0; nY
< pSalBitmap
->Height(); nY
++ )
136 for( nX
= 0; nX
< pSalBitmap
->Width(); nX
++ )
139 aPix
= pSalBitmap
->GetPixel( nY
, nX
);
140 pDestData
[0] = aPix
.GetRed();
141 pDestData
[1] = aPix
.GetGreen();
142 pDestData
[2] = aPix
.GetBlue();
145 aPix
= pSalAlpha
->GetPixel( nY
, nX
);
146 pDestData
[3] = 255 - aPix
.GetIndex();
154 return gdk_pixbuf_new_from_data( pPixbufData
,
155 GDK_COLORSPACE_RGB
, sal_True
, 8,
156 aSize
.Width(), aSize
.Height(),
158 reinterpret_cast<GdkPixbufDestroyNotify
>(g_free
),
163 static void oustring_delete (gpointer data
,
164 GClosure
* /* closure */)
166 OUString
*pURL
= static_cast<OUString
*>(data
);
171 static void add_item( GtkMenuShell
*pMenuShell
, const char *pAsciiURL
,
172 OUString
*pOverrideLabel
,
173 sal_uInt16 nResId
, GCallback pFnCallback
)
175 OUString
*pURL
= new OUString (OStringToOUString( pAsciiURL
,
176 RTL_TEXTENCODING_UTF8
));
179 aLabel
= OUStringToOString (*pOverrideLabel
, RTL_TEXTENCODING_UTF8
);
182 aLabel
= OUStringToOString (ShutdownIcon::GetUrlDescription( *pURL
),
183 RTL_TEXTENCODING_UTF8
);
186 GdkPixbuf
*pPixbuf
= ResIdToPixbuf( SV_ICON_SMALL_START
+ nResId
);
187 GtkWidget
*pImage
= gtk_image_new_from_pixbuf( pPixbuf
);
188 g_object_unref( G_OBJECT( pPixbuf
) );
190 GtkWidget
*pMenuItem
= gtk_image_menu_item_new_with_label( aLabel
.getStr() );
191 gtk_image_menu_item_set_image( GTK_IMAGE_MENU_ITEM( pMenuItem
), pImage
);
192 g_signal_connect_data( pMenuItem
, "activate", pFnCallback
, pURL
,
193 oustring_delete
, GConnectFlags(0));
195 gtk_menu_shell_append( pMenuShell
, pMenuItem
);
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
;
204 static void add_ugly_db_item( GtkMenuShell
*pMenuShell
, const char *pAsciiURL
,
205 sal_uInt16 nResId
, GCallback pFnCallback
)
207 SvtDynamicMenuOptions aOpt
;
208 Sequence
< Sequence
< PropertyValue
> > aMenu
= aOpt
.GetMenu( E_NEWMENU
);
209 for ( sal_Int32 n
=0; n
<aMenu
.getLength(); n
++ )
212 OUString aDescription
;
213 Sequence
< PropertyValue
>& aEntry
= aMenu
[n
];
214 for ( sal_Int32 m
=0; m
<aEntry
.getLength(); m
++ )
216 if ( aEntry
[m
].Name
== "URL" )
217 aEntry
[m
].Value
>>= aURL
;
218 if ( aEntry
[m
].Name
== "Title" )
219 aEntry
[m
].Value
>>= aDescription
;
222 if ( aURL
== BASE_URL
&& !aDescription
.isEmpty() )
224 add_item (pMenuShell
, pAsciiURL
, &aDescription
, nResId
, pFnCallback
);
231 add_image_menu_item( GtkMenuShell
*pMenuShell
,
232 const gchar
*stock_id
,
233 const OUString
& aLabel
,
234 GCallback activate_cb
)
236 OString aUtfLabel
= OUStringToOString (aLabel
, RTL_TEXTENCODING_UTF8
);
239 pImage
= gtk_image_new_from_stock( stock_id
, GTK_ICON_SIZE_MENU
);
241 GtkWidget
*pMenuItem
;
242 pMenuItem
= gtk_image_menu_item_new_with_label( aUtfLabel
.getStr() );
243 gtk_image_menu_item_set_image( GTK_IMAGE_MENU_ITEM( pMenuItem
), pImage
);
245 gtk_menu_shell_append( pMenuShell
, pMenuItem
);
246 g_signal_connect( pMenuItem
, "activate", activate_cb
, NULL
);
251 static void populate_menu( GtkWidget
*pMenu
)
253 ShutdownIcon
*pShutdownIcon
= ShutdownIcon::getInstance();
254 GtkMenuShell
*pMenuShell
= GTK_MENU_SHELL( pMenu
);
255 SvtModuleOptions aModuleOptions
;
257 if ( aModuleOptions
.IsWriter() )
258 add_item (pMenuShell
, WRITER_URL
, NULL
,
259 SV_ICON_ID_TEXT
, G_CALLBACK( open_url_cb
));
261 if ( aModuleOptions
.IsCalc() )
262 add_item (pMenuShell
, CALC_URL
, NULL
,
263 SV_ICON_ID_SPREADSHEET
, G_CALLBACK( open_url_cb
));
265 if ( aModuleOptions
.IsImpress() )
266 add_item (pMenuShell
, IMPRESS_URL
, NULL
,
267 SV_ICON_ID_PRESENTATION
, G_CALLBACK( open_url_cb
));
269 if ( aModuleOptions
.IsDraw() )
270 add_item (pMenuShell
, DRAW_URL
, NULL
,
271 SV_ICON_ID_DRAWING
, G_CALLBACK( open_url_cb
));
273 if ( aModuleOptions
.IsDataBase() )
274 add_ugly_db_item (pMenuShell
, BASE_URL
,
275 SV_ICON_ID_DATABASE
, G_CALLBACK( open_url_cb
));
277 if ( aModuleOptions
.IsMath() )
278 add_item (pMenuShell
, MATH_URL
, NULL
,
279 SV_ICON_ID_FORMULA
, G_CALLBACK( open_url_cb
));
281 OUString aULabel
= pShutdownIcon
->GetResString( STR_QUICKSTART_FROMTEMPLATE
);
282 add_item (pMenuShell
, "dummy", &aULabel
,
283 SV_ICON_ID_TEMPLATE
, G_CALLBACK( open_template_cb
));
285 GtkWidget
*pMenuItem
;
287 pMenuItem
= gtk_separator_menu_item_new();
288 gtk_menu_shell_append( pMenuShell
, pMenuItem
);
290 pOpenMenuItem
= add_image_menu_item
291 (pMenuShell
, GTK_STOCK_OPEN
,
292 pShutdownIcon
->GetResString( STR_QUICKSTART_FILEOPEN
),
293 G_CALLBACK( open_file_cb
));
296 pMenuItem
= gtk_separator_menu_item_new();
297 gtk_menu_shell_append( pMenuShell
, pMenuItem
);
299 pDisableMenuItem
= add_image_menu_item
300 ( pMenuShell
, GTK_STOCK_CLOSE
,
301 pShutdownIcon
->GetResString( STR_QUICKSTART_PRELAUNCH_UNX
),
302 G_CALLBACK( systray_disable_cb
) );
304 pMenuItem
= gtk_separator_menu_item_new();
305 gtk_menu_shell_append( pMenuShell
, pMenuItem
);
307 pExitMenuItem
= add_image_menu_item
308 ( pMenuShell
, GTK_STOCK_QUIT
,
309 pShutdownIcon
->GetResString( STR_QUICKSTART_EXIT
),
310 G_CALLBACK( exit_quickstarter_cb
) );
312 gtk_widget_show_all( pMenu
);
315 static void refresh_menu( GtkWidget
*pMenu
)
318 populate_menu( pMenu
);
320 bool bModal
= ShutdownIcon::bModalMode
;
321 gtk_widget_set_sensitive( pExitMenuItem
, !bModal
);
322 gtk_widget_set_sensitive( pOpenMenuItem
, !bModal
);
323 gtk_widget_set_sensitive( pDisableMenuItem
, !bModal
);
326 static gboolean
display_menu_cb( GtkWidget
*,
327 GdkEventButton
*event
, GtkWidget
*pMenu
)
329 if (event
->button
== 2)
332 refresh_menu( pMenu
);
334 gtk_menu_popup( GTK_MENU( pMenu
), NULL
, NULL
,
335 gtk_status_icon_position_menu
, pTrayIcon
,
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
349 static void notify_file_changed(GFileMonitor
* /*gfilemonitor*/, GFile
* /*arg1*/,
350 GFile
* /*arg2*/, GFileMonitorEvent event_type
, gpointer
/*user_data*/)
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
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 exit_quickstarter_cb(GTK_WIDGET(pTrayIcon
));
369 void plugin_init_sys_tray()
371 ::SolarMutexGuard aGuard
;
373 if( /* need gtk_status to resolve */
374 (gtk_check_version( 2, 10, 0 ) != NULL
) ||
375 /* we need the vcl plugin and mainloop initialized */
376 !g_type_from_name( "GdkDisplay" ) )
380 ShutdownIcon
*pShutdownIcon
= ShutdownIcon::getInstance();
382 aLabel
= OUStringToOString (
383 pShutdownIcon
->GetResString( STR_QUICKSTART_TIP
),
384 RTL_TEXTENCODING_UTF8
);
386 pVCLResMgr
= ResMgr::CreateResMgr("vcl");
388 GdkPixbuf
*pPixbuf
= ResIdToPixbuf( SV_ICON_SMALL_START
+ SV_ICON_ID_OFFICE
);
389 pTrayIcon
= gtk_status_icon_new_from_pixbuf(pPixbuf
);
390 g_object_unref( pPixbuf
);
392 g_object_set (pTrayIcon
, "title", aLabel
.getStr(),
393 "tooltip_text", aLabel
.getStr(), NULL
);
395 GtkWidget
*pMenu
= gtk_menu_new();
396 g_signal_connect(pTrayIcon
, "button-press-event",
397 G_CALLBACK(display_menu_cb
), pMenu
);
398 g_signal_connect (pMenu
, "deactivate",
399 G_CALLBACK (menu_deactivate_cb
), NULL
);
402 pShutdownIcon
->SetVeto( true );
403 ShutdownIcon::addTerminateListener();
407 OUString sLibraryFileUrl
;
408 if (osl::Module::getUrlFromAddress(plugin_init_sys_tray
, sLibraryFileUrl
))
409 pFile
= g_file_new_for_uri(OUStringToOString(sLibraryFileUrl
, RTL_TEXTENCODING_UTF8
).getStr());
413 if ((pMonitor
= g_file_monitor_file(pFile
, G_FILE_MONITOR_NONE
, NULL
, NULL
)))
414 g_signal_connect(pMonitor
, "changed", reinterpret_cast<GCallback
>(notify_file_changed
), NULL
);
415 g_object_unref(pFile
);
420 void plugin_shutdown_sys_tray()
422 ::SolarMutexGuard aGuard
;
429 g_signal_handlers_disconnect_by_func(pMonitor
,
430 reinterpret_cast<gpointer
>(¬ify_file_changed
), pMonitor
);
431 g_file_monitor_cancel(pMonitor
);
432 g_object_unref(pMonitor
);
437 g_object_unref(pTrayIcon
);
440 pExitMenuItem
= NULL
;
441 pOpenMenuItem
= NULL
;
442 pDisableMenuItem
= NULL
;
445 #endif // ENABLE_QUICKSTART_APPLET
447 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */