~ubuntu-branches/ubuntu/vivid/guayadeque/vivid

« back to all changes in this revision

Viewing changes to .pc/05-wx3.0.patch/src/MainApp.cpp

  • Committer: Package Import Robot
  • Author(s): Artur Rona
  • Date: 2014-12-29 01:09:05 UTC
  • mfrom: (12.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20141229010905-r5m8w9224bt0nm05
Tags: 0.3.7~ds0-2.1ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - debian/control, debian/patches/05-diable-indicate.patch:
    + Disable indicator, as mpris only is needed for sound
      menu integration?!

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -------------------------------------------------------------------------------- //
 
2
//      Copyright (C) 2008-2013 J.Rios
 
3
//      anonbeat@gmail.com
 
4
//
 
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)
 
8
//    any later version.
 
9
//
 
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.
 
14
//
 
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
 
19
//
 
20
// -------------------------------------------------------------------------------- //
 
21
#include "MainApp.h"
 
22
 
 
23
#include "Config.h"
 
24
#include "Images.h"
 
25
#include "MainFrame.h"
 
26
#include "mpris.h"
 
27
#include "SplashWin.h"
 
28
#include "Settings.h"
 
29
#include "Utils.h"
 
30
 
 
31
#include "wx/clipbrd.h"
 
32
#include <wx/curl/base.h>
 
33
#include <wx/image.h>
 
34
#include <wx/tooltip.h>
 
35
#include <wx/stdpaths.h>
 
36
#include <wx/utils.h>
 
37
 
 
38
#include <wx/debugrpt.h>
 
39
 
 
40
IMPLEMENT_APP( guMainApp );
 
41
 
 
42
// -------------------------------------------------------------------------------- //
 
43
guMainApp::guMainApp() : wxApp()
 
44
{
 
45
//    m_Db = NULL;
 
46
    m_DbCache = NULL;
 
47
 
 
48
#if wxUSE_ON_FATAL_EXCEPTION    // Thanks TheBigRed
 
49
        wxHandleFatalExceptions();
 
50
#endif
 
51
 
 
52
    if( !wxDirExists( guPATH_CONFIG ) )
 
53
    {
 
54
        wxMkdir( guPATH_CONFIG, 0770 );
 
55
        guLogMessage( wxT( "Created the configuration folder" ) );
 
56
    }
 
57
 
 
58
    if( !wxDirExists( guPATH_LYRICS ) )
 
59
    {
 
60
        wxMkdir( guPATH_LYRICS, 0770 );
 
61
        guLogMessage( wxT( "Created the lyrics folder" ) );
 
62
    }
 
63
 
 
64
    if( !wxDirExists( guPATH_COLLECTIONS ) )
 
65
    {
 
66
        wxMkdir( guPATH_COLLECTIONS, 0770 );
 
67
        guLogMessage( wxT( "Created the collections folder" ) );
 
68
    }
 
69
 
 
70
    if( !wxDirExists( guPATH_RADIOS ) )
 
71
    {
 
72
        wxMkdir( guPATH_RADIOS, 0770 );
 
73
        guLogMessage( wxT( "Created the Radios folder" ) );
 
74
    }
 
75
 
 
76
    if( !wxDirExists( guPATH_JAMENDO ) )
 
77
    {
 
78
        wxMkdir( guPATH_JAMENDO, 0770 );
 
79
        wxMkdir( guPATH_JAMENDO_COVERS, 0770 );
 
80
        guLogMessage( wxT( "Created the Jamendo folder" ) );
 
81
    }
 
82
 
 
83
    if( !wxDirExists( guPATH_MAGNATUNE ) )
 
84
    {
 
85
        wxMkdir( guPATH_MAGNATUNE, 0770 );
 
86
        wxMkdir( guPATH_MAGNATUNE_COVERS, 0770 );
 
87
        guLogMessage( wxT( "Created the Magnatune folder" ) );
 
88
    }
 
89
 
 
90
    if( !wxDirExists( guPATH_PODCASTS ) )
 
91
    {
 
92
        wxMkdir( guPATH_PODCASTS, 0770 );
 
93
        guLogMessage( wxT( "Created the Podcasts folder" ) );
 
94
    }
 
95
 
 
96
    if( !wxDirExists( guPATH_DEVICES ) )
 
97
    {
 
98
        wxMkdir( guPATH_DEVICES, 0770 );
 
99
        guLogMessage( wxT( "Created the Devices folder" ) );
 
100
    }
 
101
 
 
102
    if( !wxDirExists( guPATH_LAYOUTS ) )
 
103
    {
 
104
        wxMkdir( guPATH_LAYOUTS, 0770 );
 
105
        guLogMessage( wxT( "Created the Layouts folder" ) );
 
106
    }
 
107
 
 
108
    if( !wxFileExists( guPATH_CONFIG_FILENAME ) )
 
109
    {
 
110
        if( wxFileExists( wxT( "/usr/share/guayadeque/guayadeque.default.conf" ) ) )
 
111
        {
 
112
            wxCopyFile( wxT( "/usr/share/guayadeque/guayadeque.default.conf" ),
 
113
                        guPATH_CONFIG_FILENAME, false );
 
114
        }
 
115
        else if( wxFileExists( wxT( "/usr/local/share/guayadeque/guayadeque.default.conf" ) ) )
 
116
        {
 
117
            wxCopyFile( wxT( "/usr/local/share/guayadeque/guayadeque.default.conf" ),
 
118
                        guPATH_CONFIG_FILENAME, false );
 
119
        }
 
120
        guLogMessage( wxT( "Created the default configuration file" ) );
 
121
    }
 
122
 
 
123
    if( !wxFileExists( guPATH_EQUALIZERS_FILENAME ) )
 
124
    {
 
125
        if( wxFileExists( wxT( "/usr/share/guayadeque/equalizers.default.conf" ) ) )
 
126
        {
 
127
            wxCopyFile( wxT( "/usr/share/guayadeque/equalizers.default.conf" ),
 
128
                        guPATH_EQUALIZERS_FILENAME, false );
 
129
        }
 
130
        else if( wxFileExists( wxT( "/usr/local/share/guayadeque/equalizers.default.conf" ) ) )
 
131
        {
 
132
            wxCopyFile( wxT( "/usr/local/share/guayadeque/equalizers.default.conf" ),
 
133
                        guPATH_EQUALIZERS_FILENAME, false );
 
134
        }
 
135
        guLogMessage( wxT( "Created the default equalizers file" ) );
 
136
    }
 
137
 
 
138
    if( !wxFileExists( guPATH_LYRICS_SOURCES_FILENAME ) )
 
139
    {
 
140
        if( wxFileExists( wxT( "/usr/share/guayadeque/lyrics_sources.xml" ) ) )
 
141
        {
 
142
            wxCopyFile( wxT( "/usr/share/guayadeque/lyrics_sources.xml" ),
 
143
                        guPATH_LYRICS_SOURCES_FILENAME, false );
 
144
        }
 
145
        else if( wxFileExists( wxT( "/usr/local/share/guayadeque/lyrics_sources.xml" ) ) )
 
146
        {
 
147
            wxCopyFile( wxT( "/usr/local/share/guayadeque/lyrics_sources.xml" ),
 
148
                        guPATH_LYRICS_SOURCES_FILENAME, false );
 
149
        }
 
150
        guLogMessage( wxT( "Created the default lyrics sources file" ) );
 
151
    }
 
152
 
 
153
    m_Config = new guConfig();
 
154
    m_Config->Set( m_Config );
 
155
 
 
156
}
 
157
 
 
158
// -------------------------------------------------------------------------------- //
 
159
guMainApp::~guMainApp()
 
160
{
 
161
    if( m_SingleInstanceChecker )
 
162
        delete m_SingleInstanceChecker;
 
163
 
 
164
    if( m_DbCache )
 
165
    {
 
166
        m_DbCache->Close();
 
167
        delete m_DbCache;
 
168
    }
 
169
 
 
170
    // config
 
171
    if( m_Config )
 
172
      delete m_Config;
 
173
}
 
174
 
 
175
 
 
176
// -------------------------------------------------------------------------------- //
 
177
// Its done this way to avoid the warning of temporary address
 
178
void inline Append_String( DBusMessageIter * iter, const char * str )
 
179
{
 
180
    dbus_message_iter_append_basic( iter, DBUS_TYPE_STRING, &str );
 
181
}
 
182
 
 
183
// -------------------------------------------------------------------------------- //
 
184
bool SendFilesByMPRIS( const int argc, wxChar * argv[] )
 
185
{
 
186
    DBusError dberr;
 
187
    DBusConnection * dbconn;
 
188
    DBusMessage * dbmsg, * dbreply;
 
189
    DBusMessageIter dbiter;
 
190
    DBusMessageIter dbitertracks;
 
191
 
 
192
    dbus_error_init( &dberr );
 
193
    dbconn = dbus_bus_get( DBUS_BUS_SESSION, &dberr );
 
194
 
 
195
    if( dbus_error_is_set( &dberr ) )
 
196
    {
 
197
         printf( "getting session bus failed: %s\n", dberr.message );
 
198
         dbus_error_free( &dberr );
 
199
         return false;
 
200
    }
 
201
 
 
202
 
 
203
    dbmsg = dbus_message_new_method_call( GUAYADEQUE_MPRIS_SERVICENAME,
 
204
                                          GUAYADEQUE_MPRIS_TRACKLIST_PATH,
 
205
                                          GUAYADEQUE_MPRIS_INTERFACE,
 
206
                                          "AddTracks" );
 
207
    if( dbmsg == NULL )
 
208
    {
 
209
         guLogError( wxT( "Couldn’t create a DBusMessage" ) );
 
210
         return false;
 
211
    }
 
212
 
 
213
    dbus_message_iter_init_append( dbmsg, &dbiter );
 
214
    dbus_message_iter_open_container( &dbiter, DBUS_TYPE_ARRAY, "s", &dbitertracks );
 
215
 
 
216
    wxString FilePath;
 
217
    int index;
 
218
    for( index = 1; index < argc; index++ )
 
219
    {
 
220
        FilePath = argv[ index ];
 
221
        //guLogMessage( wxT( "Trying to add file '%s'" ), argv[ index ] );
 
222
 
 
223
        Append_String( &dbitertracks, FilePath.char_str() );
 
224
    }
 
225
 
 
226
    dbus_message_iter_close_container( &dbiter, &dbitertracks );
 
227
 
 
228
    dbus_bool_t PlayTrack = false;
 
229
    dbus_message_iter_append_basic( &dbiter, DBUS_TYPE_BOOLEAN, &PlayTrack );
 
230
 
 
231
    dbreply = dbus_connection_send_with_reply_and_block( dbconn, dbmsg, 5000, &dberr );
 
232
    if( dbus_error_is_set( &dberr ) )
 
233
    {
 
234
          guLogMessage( wxT( "Error adding files: '%s'" ), wxString( dberr.message, wxConvUTF8 ).c_str() );
 
235
          dbus_message_unref( dbmsg );
 
236
          dbus_error_free( &dberr );
 
237
          return false;
 
238
    }
 
239
 
 
240
    if( dbreply )
 
241
        dbus_message_unref( dbreply );
 
242
 
 
243
    /* Don’t need this anymore */
 
244
    dbus_message_unref( dbmsg );
 
245
 
 
246
    dbus_connection_unref( dbconn );
 
247
 
 
248
    return true;
 
249
}
 
250
 
 
251
// -------------------------------------------------------------------------------- //
 
252
bool MakeWindowVisible( void )
 
253
{
 
254
    DBusError dberr;
 
255
    DBusConnection * dbconn;
 
256
    DBusMessage * dbmsg, * dbreply;
 
257
 
 
258
    dbus_error_init( &dberr );
 
259
    dbconn = dbus_bus_get( DBUS_BUS_SESSION, &dberr );
 
260
 
 
261
    if( dbus_error_is_set( &dberr ) )
 
262
    {
 
263
         printf( "getting session bus failed: %s\n", dberr.message );
 
264
         dbus_error_free( &dberr );
 
265
         return false;
 
266
    }
 
267
 
 
268
 
 
269
    dbmsg = dbus_message_new_method_call( "org.mpris.MediaPlayer2.guayadeque",
 
270
                                          "/org/mpris/MediaPlayer2",
 
271
                                          "org.mpris.MediaPlayer2",
 
272
                                          "Raise" );
 
273
    if( dbmsg == NULL )
 
274
    {
 
275
         guLogError( wxT( "Couldn’t create a DBusMessage" ) );
 
276
         return false;
 
277
    }
 
278
 
 
279
    dbreply = dbus_connection_send_with_reply_and_block( dbconn, dbmsg, 5000, &dberr );
 
280
    if( dbus_error_is_set( &dberr ) )
 
281
    {
 
282
          guLogMessage( wxT( "Error showing window" ) );
 
283
          dbus_message_unref( dbmsg );
 
284
          dbus_error_free( &dberr );
 
285
          return false;
 
286
    }
 
287
 
 
288
    if( dbreply )
 
289
        dbus_message_unref( dbreply );
 
290
 
 
291
    /* Don’t need this anymore */
 
292
    dbus_message_unref( dbmsg );
 
293
 
 
294
    dbus_connection_unref( dbconn );
 
295
 
 
296
    return true;
 
297
}
 
298
 
 
299
// -------------------------------------------------------------------------------- //
 
300
bool guMainApp::OnInit()
 
301
{
 
302
    guRandomInit();
 
303
 
 
304
    wxLog::SetActiveTarget( new wxLogStderr( stdout ) );
 
305
 
 
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() )
 
310
    {
 
311
        if( argc > 1 )
 
312
        {
 
313
            int RetryCnt = 0;
 
314
            while( RetryCnt++ < 25 )
 
315
            {
 
316
                if( SendFilesByMPRIS( argc, argv ) )
 
317
                {
 
318
                    break;
 
319
                }
 
320
                wxMilliSleep( 100 );
 
321
            }
 
322
        }
 
323
 
 
324
        MakeWindowVisible();
 
325
 
 
326
        guLogMessage( wxT( "Another program instance is already running, aborting." ) );
 
327
        return false;
 
328
    }
 
329
 
 
330
    // Init all image handlers
 
331
    wxInitAllImageHandlers();
 
332
 
 
333
    // If enabled Show the Splash Screen on Startup
 
334
    if( m_Config->ReadBool( wxT( "ShowSplashScreen" ), true, wxT( "general" ) ) )
 
335
    {
 
336
        guSplashFrame * SplashFrame = new guSplashFrame( 0 );
 
337
        if( !SplashFrame )
 
338
            guLogError( wxT( "Could not create splash object" ) );
 
339
        SplashFrame->Show( true );
 
340
        wxYield();
 
341
        //wxMilliSleep( 300 );
 
342
    }
 
343
 
 
344
    // Use the primary clipboard which is shared with other applications
 
345
    wxTheClipboard->UsePrimarySelection( false );
 
346
 
 
347
    // Init the wxCurl Lib
 
348
    wxCurlBase::Init();
 
349
 
 
350
    int LangId = m_Config->ReadNum( wxT( "Language" ), wxLANGUAGE_DEFAULT, wxT( "general" ) );
 
351
    if( m_Locale.Init( LangId, wxLOCALE_CONV_ENCODING ) )
 
352
    {
 
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() );
 
356
    }
 
357
    else
 
358
    {
 
359
        const wxLanguageInfo * LangInfo = wxLocale::GetLanguageInfo( LangId );
 
360
        if( LangInfo )
 
361
        {
 
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() );
 
365
        }
 
366
        else
 
367
        {
 
368
            guLogError( wxT( "Could not initialize the translations engine for (%d)" ), LangId );
 
369
        }
 
370
    }
 
371
 
 
372
    // Enable tooltips
 
373
    wxToolTip::Enable( true );
 
374
 
 
375
    m_DbCache = new guDbCache( guPATH_DBCACHE );
 
376
    if( !m_DbCache )
 
377
    {
 
378
        guLogError( wxT( "Could not open the guayadeque cache database" ) );
 
379
    }
 
380
 
 
381
    m_DbCache->SetDbCache();
 
382
 
 
383
    // Initialize the MainFrame object
 
384
    guMainFrame * Frame = new guMainFrame( 0, m_DbCache );
 
385
    SetTopWindow( Frame );
 
386
    //Frame->SetMainFrame();
 
387
    wxIcon MainIcon;
 
388
    MainIcon.CopyFromBitmap( guImage( guIMAGE_INDEX_guayadeque ) );
 
389
    Frame->SetIcon( MainIcon );
 
390
 
 
391
    // If Minimize is enabled minimized or hide it if Taskbar Icon is enabled
 
392
    if( m_Config->ReadBool( wxT( "StartMinimized" ), false, wxT( "general" ) ) )
 
393
    {
 
394
        if( m_Config->ReadBool( wxT( "ShowTaskBarIcon" ), false, wxT( "general" ) ) &&
 
395
            m_Config->ReadBool( wxT( "CloseToTaskBar" ), false, wxT( "general" ) ) )
 
396
        {
 
397
            Frame->Show( false );
 
398
            //Frame->Hide();
 
399
        }
 
400
        else
 
401
        {
 
402
            Frame->Show();
 
403
            Frame->Iconize( true );
 
404
        }
 
405
    }
 
406
    else
 
407
    {
 
408
        Frame->Show();
 
409
    }
 
410
 
 
411
    return true;
 
412
}
 
413
 
 
414
// -------------------------------------------------------------------------------- //
 
415
int guMainApp::OnExit()
 
416
{
 
417
    // Shutdown the wxCurl Lib
 
418
    wxCurlBase::Shutdown();
 
419
 
 
420
    return 0;
 
421
}
 
422
 
 
423
// -------------------------------------------------------------------------------- //
 
424
void guMainApp::OnFatalException()
 
425
{
 
426
    wxDebugReport Report;
 
427
    wxDebugReportPreviewStd Preview;
 
428
 
 
429
    Report.AddAll();
 
430
 
 
431
    if( Preview.Show( Report ) )
 
432
        Report.Process();
 
433
}
 
434
 
 
435
// -------------------------------------------------------------------------------- //