1
// -------------------------------------------------------------------------------- //
2
// Copyright (C) 2008-2013 J.Rios
5
// This Program is free software; you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation; either version 3, or (at your option)
10
// This Program is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
// GNU General Public License for more details.
15
// You should have received a copy of the GNU General Public License
16
// along with this program; see the file LICENSE. If not, write to
17
// the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18
// http://www.gnu.org/copyleft/gpl.html
20
// -------------------------------------------------------------------------------- //
25
#include "MainFrame.h"
27
#include "SplashWin.h"
31
#include "wx/clipbrd.h"
32
#include <wx/curl/base.h>
34
#include <wx/tooltip.h>
35
#include <wx/stdpaths.h>
38
#include <wx/debugrpt.h>
40
IMPLEMENT_APP( guMainApp );
42
// -------------------------------------------------------------------------------- //
43
guMainApp::guMainApp() : wxApp()
48
#if wxUSE_ON_FATAL_EXCEPTION // Thanks TheBigRed
49
wxHandleFatalExceptions();
52
if( !wxDirExists( guPATH_CONFIG ) )
54
wxMkdir( guPATH_CONFIG, 0770 );
55
guLogMessage( wxT( "Created the configuration folder" ) );
58
if( !wxDirExists( guPATH_LYRICS ) )
60
wxMkdir( guPATH_LYRICS, 0770 );
61
guLogMessage( wxT( "Created the lyrics folder" ) );
64
if( !wxDirExists( guPATH_COLLECTIONS ) )
66
wxMkdir( guPATH_COLLECTIONS, 0770 );
67
guLogMessage( wxT( "Created the collections folder" ) );
70
if( !wxDirExists( guPATH_RADIOS ) )
72
wxMkdir( guPATH_RADIOS, 0770 );
73
guLogMessage( wxT( "Created the Radios folder" ) );
76
if( !wxDirExists( guPATH_JAMENDO ) )
78
wxMkdir( guPATH_JAMENDO, 0770 );
79
wxMkdir( guPATH_JAMENDO_COVERS, 0770 );
80
guLogMessage( wxT( "Created the Jamendo folder" ) );
83
if( !wxDirExists( guPATH_MAGNATUNE ) )
85
wxMkdir( guPATH_MAGNATUNE, 0770 );
86
wxMkdir( guPATH_MAGNATUNE_COVERS, 0770 );
87
guLogMessage( wxT( "Created the Magnatune folder" ) );
90
if( !wxDirExists( guPATH_PODCASTS ) )
92
wxMkdir( guPATH_PODCASTS, 0770 );
93
guLogMessage( wxT( "Created the Podcasts folder" ) );
96
if( !wxDirExists( guPATH_DEVICES ) )
98
wxMkdir( guPATH_DEVICES, 0770 );
99
guLogMessage( wxT( "Created the Devices folder" ) );
102
if( !wxDirExists( guPATH_LAYOUTS ) )
104
wxMkdir( guPATH_LAYOUTS, 0770 );
105
guLogMessage( wxT( "Created the Layouts folder" ) );
108
if( !wxFileExists( guPATH_CONFIG_FILENAME ) )
110
if( wxFileExists( wxT( "/usr/share/guayadeque/guayadeque.default.conf" ) ) )
112
wxCopyFile( wxT( "/usr/share/guayadeque/guayadeque.default.conf" ),
113
guPATH_CONFIG_FILENAME, false );
115
else if( wxFileExists( wxT( "/usr/local/share/guayadeque/guayadeque.default.conf" ) ) )
117
wxCopyFile( wxT( "/usr/local/share/guayadeque/guayadeque.default.conf" ),
118
guPATH_CONFIG_FILENAME, false );
120
guLogMessage( wxT( "Created the default configuration file" ) );
123
if( !wxFileExists( guPATH_EQUALIZERS_FILENAME ) )
125
if( wxFileExists( wxT( "/usr/share/guayadeque/equalizers.default.conf" ) ) )
127
wxCopyFile( wxT( "/usr/share/guayadeque/equalizers.default.conf" ),
128
guPATH_EQUALIZERS_FILENAME, false );
130
else if( wxFileExists( wxT( "/usr/local/share/guayadeque/equalizers.default.conf" ) ) )
132
wxCopyFile( wxT( "/usr/local/share/guayadeque/equalizers.default.conf" ),
133
guPATH_EQUALIZERS_FILENAME, false );
135
guLogMessage( wxT( "Created the default equalizers file" ) );
138
if( !wxFileExists( guPATH_LYRICS_SOURCES_FILENAME ) )
140
if( wxFileExists( wxT( "/usr/share/guayadeque/lyrics_sources.xml" ) ) )
142
wxCopyFile( wxT( "/usr/share/guayadeque/lyrics_sources.xml" ),
143
guPATH_LYRICS_SOURCES_FILENAME, false );
145
else if( wxFileExists( wxT( "/usr/local/share/guayadeque/lyrics_sources.xml" ) ) )
147
wxCopyFile( wxT( "/usr/local/share/guayadeque/lyrics_sources.xml" ),
148
guPATH_LYRICS_SOURCES_FILENAME, false );
150
guLogMessage( wxT( "Created the default lyrics sources file" ) );
153
m_Config = new guConfig();
154
m_Config->Set( m_Config );
158
// -------------------------------------------------------------------------------- //
159
guMainApp::~guMainApp()
161
if( m_SingleInstanceChecker )
162
delete m_SingleInstanceChecker;
176
// -------------------------------------------------------------------------------- //
177
// Its done this way to avoid the warning of temporary address
178
void inline Append_String( DBusMessageIter * iter, const char * str )
180
dbus_message_iter_append_basic( iter, DBUS_TYPE_STRING, &str );
183
// -------------------------------------------------------------------------------- //
184
bool SendFilesByMPRIS( const int argc, wxChar * argv[] )
187
DBusConnection * dbconn;
188
DBusMessage * dbmsg, * dbreply;
189
DBusMessageIter dbiter;
190
DBusMessageIter dbitertracks;
192
dbus_error_init( &dberr );
193
dbconn = dbus_bus_get( DBUS_BUS_SESSION, &dberr );
195
if( dbus_error_is_set( &dberr ) )
197
printf( "getting session bus failed: %s\n", dberr.message );
198
dbus_error_free( &dberr );
203
dbmsg = dbus_message_new_method_call( GUAYADEQUE_MPRIS_SERVICENAME,
204
GUAYADEQUE_MPRIS_TRACKLIST_PATH,
205
GUAYADEQUE_MPRIS_INTERFACE,
209
guLogError( wxT( "Couldn’t create a DBusMessage" ) );
213
dbus_message_iter_init_append( dbmsg, &dbiter );
214
dbus_message_iter_open_container( &dbiter, DBUS_TYPE_ARRAY, "s", &dbitertracks );
218
for( index = 1; index < argc; index++ )
220
FilePath = argv[ index ];
221
//guLogMessage( wxT( "Trying to add file '%s'" ), argv[ index ] );
223
Append_String( &dbitertracks, FilePath.char_str() );
226
dbus_message_iter_close_container( &dbiter, &dbitertracks );
228
dbus_bool_t PlayTrack = false;
229
dbus_message_iter_append_basic( &dbiter, DBUS_TYPE_BOOLEAN, &PlayTrack );
231
dbreply = dbus_connection_send_with_reply_and_block( dbconn, dbmsg, 5000, &dberr );
232
if( dbus_error_is_set( &dberr ) )
234
guLogMessage( wxT( "Error adding files: '%s'" ), wxString( dberr.message, wxConvUTF8 ).c_str() );
235
dbus_message_unref( dbmsg );
236
dbus_error_free( &dberr );
241
dbus_message_unref( dbreply );
243
/* Don’t need this anymore */
244
dbus_message_unref( dbmsg );
246
dbus_connection_unref( dbconn );
251
// -------------------------------------------------------------------------------- //
252
bool MakeWindowVisible( void )
255
DBusConnection * dbconn;
256
DBusMessage * dbmsg, * dbreply;
258
dbus_error_init( &dberr );
259
dbconn = dbus_bus_get( DBUS_BUS_SESSION, &dberr );
261
if( dbus_error_is_set( &dberr ) )
263
printf( "getting session bus failed: %s\n", dberr.message );
264
dbus_error_free( &dberr );
269
dbmsg = dbus_message_new_method_call( "org.mpris.MediaPlayer2.guayadeque",
270
"/org/mpris/MediaPlayer2",
271
"org.mpris.MediaPlayer2",
275
guLogError( wxT( "Couldn’t create a DBusMessage" ) );
279
dbreply = dbus_connection_send_with_reply_and_block( dbconn, dbmsg, 5000, &dberr );
280
if( dbus_error_is_set( &dberr ) )
282
guLogMessage( wxT( "Error showing window" ) );
283
dbus_message_unref( dbmsg );
284
dbus_error_free( &dberr );
289
dbus_message_unref( dbreply );
291
/* Don’t need this anymore */
292
dbus_message_unref( dbmsg );
294
dbus_connection_unref( dbconn );
299
// -------------------------------------------------------------------------------- //
300
bool guMainApp::OnInit()
304
wxLog::SetActiveTarget( new wxLogStderr( stdout ) );
306
const wxString AppName = wxString::Format( wxT( ".guayadeque/.guayadeque-%s" ), wxGetUserId().c_str() );
307
//guLogMessage( wxT( "Init: %s" ), AppName.c_str() );
308
m_SingleInstanceChecker = new wxSingleInstanceChecker( AppName );
309
if( m_SingleInstanceChecker->IsAnotherRunning() )
314
while( RetryCnt++ < 25 )
316
if( SendFilesByMPRIS( argc, argv ) )
326
guLogMessage( wxT( "Another program instance is already running, aborting." ) );
330
// Init all image handlers
331
wxInitAllImageHandlers();
333
// If enabled Show the Splash Screen on Startup
334
if( m_Config->ReadBool( wxT( "ShowSplashScreen" ), true, wxT( "general" ) ) )
336
guSplashFrame * SplashFrame = new guSplashFrame( 0 );
338
guLogError( wxT( "Could not create splash object" ) );
339
SplashFrame->Show( true );
341
//wxMilliSleep( 300 );
344
// Use the primary clipboard which is shared with other applications
345
wxTheClipboard->UsePrimarySelection( false );
347
// Init the wxCurl Lib
350
int LangId = m_Config->ReadNum( wxT( "Language" ), wxLANGUAGE_DEFAULT, wxT( "general" ) );
351
if( m_Locale.Init( LangId, wxLOCALE_CONV_ENCODING ) )
353
m_Locale.AddCatalogLookupPathPrefix( wxT( "/usr/share/locale" ) );
354
m_Locale.AddCatalog( wxT( "guayadeque" ) );
355
guLogMessage( wxT( "Initialized locale ( %s )" ), m_Locale.GetName().c_str() );
359
const wxLanguageInfo * LangInfo = wxLocale::GetLanguageInfo( LangId );
362
guLogError( wxT( "Could not initialize the translations engine for ( %s )" ), LangInfo->CanonicalName.c_str() );
363
wxStandardPaths StdPaths;
364
guLogError( wxT( "Locale directory '%s'" ), StdPaths.GetLocalizedResourcesDir( LangInfo->CanonicalName, wxStandardPaths::ResourceCat_Messages).c_str() );
368
guLogError( wxT( "Could not initialize the translations engine for (%d)" ), LangId );
373
wxToolTip::Enable( true );
375
m_DbCache = new guDbCache( guPATH_DBCACHE );
378
guLogError( wxT( "Could not open the guayadeque cache database" ) );
381
m_DbCache->SetDbCache();
383
// Initialize the MainFrame object
384
guMainFrame * Frame = new guMainFrame( 0, m_DbCache );
385
SetTopWindow( Frame );
386
//Frame->SetMainFrame();
388
MainIcon.CopyFromBitmap( guImage( guIMAGE_INDEX_guayadeque ) );
389
Frame->SetIcon( MainIcon );
391
// If Minimize is enabled minimized or hide it if Taskbar Icon is enabled
392
if( m_Config->ReadBool( wxT( "StartMinimized" ), false, wxT( "general" ) ) )
394
if( m_Config->ReadBool( wxT( "ShowTaskBarIcon" ), false, wxT( "general" ) ) &&
395
m_Config->ReadBool( wxT( "CloseToTaskBar" ), false, wxT( "general" ) ) )
397
Frame->Show( false );
403
Frame->Iconize( true );
414
// -------------------------------------------------------------------------------- //
415
int guMainApp::OnExit()
417
// Shutdown the wxCurl Lib
418
wxCurlBase::Shutdown();
423
// -------------------------------------------------------------------------------- //
424
void guMainApp::OnFatalException()
426
wxDebugReport Report;
427
wxDebugReportPreviewStd Preview;
431
if( Preview.Show( Report ) )
435
// -------------------------------------------------------------------------------- //