~gloobus-dev/gloobus-preview/trunk

« back to all changes in this revision

Viewing changes to src/gloobus-preview-singleton.cpp

  • Committer: Jordi Puigdellívol
  • Date: 2010-04-05 10:02:37 UTC
  • mfrom: (182.1.31 gloobus-preview)
  • Revision ID: jordi@badchoice-20100405100237-2hxi07cmxqwuvcxm
Merged with cahr

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <algorithm>
 
2
 
1
3
#include "gloobus-preview-singleton.h"
2
 
 
3
4
#include "gloobus-preview-interface-main.h"
4
 
#include "gloobus-preview-interface-image.h"
5
 
#include "gloobus-preview-interface-text.h"
6
 
#include "gloobus-preview-interface-music.h"
7
 
#include "gloobus-preview-interface-document.h"
8
 
#include "gloobus-preview-interface-video.h"
9
 
#include "gloobus-preview-interface-list.h"
10
 
#include "gloobus-preview-interface-free.h"
11
5
#include "gloobus-preview-interface-default.h"
 
6
#include "gloobus-preview-plugin-manager.h"
 
7
#include "gloobus-preview-window.h"
12
8
 
13
9
//// ============================= SINGLETON =========================== //
14
10
Gloobus * Gloobus::m_instance = 0;
15
11
Gloobus * Gloobus::instance()
16
 
17
 
        if (m_instance == NULL)
18
 
        {
19
 
                m_instance = new Gloobus();
20
 
        }
21
 
        return m_instance; 
 
12
{
 
13
        if (m_instance == NULL)
 
14
                m_instance = new Gloobus();
 
15
        return m_instance; 
22
16
}
23
17
 
24
18
Gloobus::Gloobus()
25
 
        :m_PManager(new pluginManager() )
 
19
        :m_plugin(0)
 
20
        ,m_fileinfo(0)
26
21
        ,m_gwindow(0)
27
22
        ,m_fullscreen(false)
28
23
        ,m_gloobus_interface(0)
29
24
        ,m_timeoutID(0)
30
25
        ,m_error(0)
 
26
        ,m_config(0)
31
27
{
32
28
        g_debug("Creating Gloobus class");
 
29
        PluginManager::load_plugins();
33
30
}
34
31
 
35
32
//// ============================== INIT ================================ //
37
34
{
38
35
        m_filename              = filename;
39
36
        m_gwindow               = win;
40
 
        
41
 
        m_PManager->load_plugins        ();                     //Plugin manager loads the .so
42
37
 
43
38
        GdkScreen *screen = gdk_screen_get_default ();
44
39
        m_gwindow->set_screen_size(gdk_screen_get_width(screen),
48
43
// Unload all the plugins
49
44
void Gloobus::end()
50
45
{
51
 
        m_PManager->unload_plugins();                                           // Unload Plugins
52
 
}
53
 
 
54
 
void Gloobus::show_preview(char * target)
55
 
{
56
 
        string filename;
57
 
        gloobusWin * win = new gloobusWin();
58
 
        win->create_window( DEFAULT_WIDTH, DEFAULT_HEIGHT );
59
 
        
60
 
        //Events Handlers
61
 
        g_signal_connect (win->get_window_pointer(), "key-press-event",
62
 
                G_CALLBACK(key_press), NULL);
63
 
 
64
 
        win->create_toolbar();
65
 
        g_signal_connect (win->get_toolbar_pointer(),"key-press-event",
66
 
                                          G_CALLBACK(key_press), NULL);
67
 
                
68
 
        if(target) {
69
 
                filename = target;
70
 
 
71
 
                // check relative path:
72
 
                if (filename.c_str()[0] != '/') {
73
 
                        filename = "./" + filename;
74
 
                }
75
 
                
76
 
                // strip final slash:
77
 
                if (filename.c_str()[filename.size()-1] == '/') {
78
 
                        filename = filename.substr(0,filename.size()-1);
79
 
                }
80
 
        } else {
81
 
                g_debug("Getting file from clipboard");
82
 
                GtkClipboard *clip      = gtk_clipboard_get ( GDK_SELECTION_CLIPBOARD );
83
 
                gchar **uri             = gtk_clipboard_wait_for_uris ( clip );
84
 
                if(uri == NULL) {\
85
 
                        m_error = g_error_new(g_quark_from_string ("Gloobus"), 1,
86
 
                                _("There is no file in clipboard"));
87
 
                } else {
88
 
                        filename = g_filename_from_uri(uri[0], NULL, NULL);
89
 
                }
90
 
        }
91
 
        init( filename , win );
92
 
        load_folder_filenames();
93
 
        if( !m_error ) {
94
 
                g_debug("Filename: %s", filename.c_str());
95
 
                if( !search_actual_filename()) {
96
 
                        m_error = g_error_new(g_quark_from_string ("Gloobus"), 2,
97
 
                                _("File not found"));
98
 
                } else if(create_plugin()) {
99
 
 
100
 
                        int w = get_width () + SHADOW_WIDTH * 2;
101
 
                        int h = get_height() + SHADOW_WIDTH * 2 + BOTTOM_HEIGHT + HEADER_HEIGHT;
102
 
 
103
 
                        win->set_size( w, h );
104
 
                        win->set_title( filename );
105
 
                        win->add_common_buttons();
106
 
                        win->show_all();
107
 
 
108
 
                        draw_plugin (); //Its drawn on top so buttons in fullscreen are hidden
109
 
                } else {
110
 
                        m_error = m_gloobus_interface->get_error();
111
 
                }
112
 
        }
113
 
        if(m_error)
114
 
                show_error();
115
 
  
116
 
    gtk_main ();
117
 
 
118
 
        destroy_plugin();//Destroy current plugin
119
 
        end();                   //Unload All plugins before leave
120
 
}
121
 
 
122
 
void Gloobus::show_supported_filetypes  ( )
123
 
{
124
 
        m_PManager->load_plugins        ();                     //Plugin manager loads the .so
125
 
        m_PManager->show_supported_filetypes();
126
 
        m_PManager->unload_plugins();           // Unload Plugins
127
 
}
128
 
 
129
 
void Gloobus::show_help  ( )
130
 
{
131
 
        printf(
132
 
                "%s:\n"
133
 
                "   gloobus-preview [%s...] [URI]\n\n",
134
 
                _("Usage"), _("OPTION") );
135
 
 
136
 
        printf(
137
 
                "   %s\n\n", _(
138
 
                "If URI is not provided Gloobus-Preview checks clipboard "
139
 
                "for filename"));
140
 
 
141
 
        printf("%s:\n", _("Help options") );
142
 
        printf("   -h, --help                   %s\n",
143
 
                _("Show help options") );
144
 
        
145
 
        printf("%s:\n", _("Application options") );
146
 
        printf("   -d, --debug                  %s\n",
147
 
                _("Show debug messages") );
148
 
        printf("   --show-supported-filetypes   %s\n",
149
 
                _("Show supported filetypes") );
150
 
 
151
 
        printf("\n");
152
 
}
153
 
 
154
 
void Gloobus::show_error( bool idefault ) {
155
 
        //g_warning(m_error->message);
156
 
        if(idefault || m_gloobus_interface ) {
157
 
                g_debug("Loading iDefault");
158
 
                
159
 
                m_class = "default";
160
 
                m_gloobus_interface = new iDefault();
161
 
                m_gloobus_interface->set_filename(m_filename);
162
 
                m_gloobus_interface->load();
163
 
 
164
 
                m_gwindow->set_size(get_width() +  SHADOW_WIDTH*2, get_height()+ SHADOW_WIDTH*2 + BOTTOM_HEIGHT+HEADER_HEIGHT); 
165
 
                m_gwindow->set_title              (m_filename);
166
 
                m_gwindow->add_common_buttons ();
167
 
                m_gwindow->show_all                       ();
168
 
        
169
 
                draw_plugin();
170
 
        } else {
171
 
                g_debug("Loading error dialog");
172
 
                gtk_widget_destroy      (GTK_WIDGET(m_gwindow->get_container_pointer()));
173
 
                gtk_widget_destroy      (GTK_WIDGET(m_gwindow->get_toolbar_pointer()));
174
 
 
175
 
                m_gwindow->create_toolbar();
176
 
                m_gwindow->create_container();
177
 
                
178
 
                m_gwindow->set_size(
179
 
                        384 + SHADOW_WIDTH * 2,
180
 
                        128 + SHADOW_WIDTH * 2 /*+ BOTTOM_HEIGHT*/ + HEADER_HEIGHT);
181
 
                m_gwindow->set_title(m_filename);
182
 
                m_gwindow->add_common_buttons();
183
 
                //GtkWidget * box = gtk_vbox_new(false, 0);
184
 
                GtkWidget * icon = gtk_image_new_from_stock(GTK_STOCK_DIALOG_ERROR,
185
 
                        GTK_ICON_SIZE_DIALOG);
186
 
                GtkWidget * label = gtk_label_new(m_error->message);
187
 
                
188
 
                PangoAttrList *info_attrs = pango_attr_list_new();
189
 
                pango_attr_list_insert(info_attrs,
190
 
                        pango_attr_foreground_new(53889,14927,253));
191
 
                pango_attr_list_insert(info_attrs,
192
 
                        pango_attr_weight_new(PANGO_WEIGHT_BOLD));
193
 
                pango_attr_list_insert(info_attrs, pango_attr_size_new(14*PANGO_SCALE));
194
 
                gtk_widget_set_size_request(label, 256, 128);
195
 
                gtk_label_set_attributes(GTK_LABEL(label), info_attrs);
196
 
                gtk_label_set_line_wrap(GTK_LABEL(label), true);
197
 
                gtk_misc_set_alignment(GTK_MISC(label), 0 , 0.5);
198
 
                
199
 
                // GTK_ICON_SIZE_DIALOG is 40, for all themes?
200
 
                // 128-40 /2 = 44 - place icon in the middle
201
 
                gtk_fixed_put(GTK_FIXED(m_gwindow->get_container_pointer()),
202
 
                        icon, SHADOW_WIDTH+44, HEADER_HEIGHT + SHADOW_WIDTH + 44);
203
 
                gtk_fixed_put(GTK_FIXED(m_gwindow->get_container_pointer()),
204
 
                        label, SHADOW_WIDTH+128, HEADER_HEIGHT + SHADOW_WIDTH);
205
 
                gtk_widget_show_all(icon);
206
 
                gtk_widget_show_all(label);
207
 
                m_gwindow->show_all();
208
 
        }
 
46
        PluginManager::unload_plugins();
209
47
}
210
48
 
211
49
// =============================== CREATE PLUGIN ======================= //
214
52
// can be called with directly on this variable
215
53
bool Gloobus::create_plugin( )
216
54
{
217
 
        m_plugin_name = "";
218
 
        m_class           = "";
219
 
        
220
 
        m_plugin_name    = m_PManager->search_for_plugin(m_filename);
221
 
        m_class                  = m_PManager->get_classtype ();
222
 
        g_debug("Creating plugin class: %s",m_class.c_str());
223
 
        
224
 
        if(m_class == "image") {
225
 
                g_debug("IMAGE (%s)",m_plugin_name.c_str());
226
 
                m_gloobus_interface = factory_image[m_plugin_name.c_str()](); //Polymorphism
227
 
        } else if(m_class == "music") {
228
 
                g_debug("MUSIC (%s)",m_plugin_name.c_str());
229
 
                m_gloobus_interface = factory_music[m_plugin_name.c_str()](); //Polymorphism
230
 
        } else if(m_class == "text") {
231
 
                g_debug("TEXT (%s)",m_plugin_name.c_str());
232
 
                m_gloobus_interface = factory_text[m_plugin_name.c_str()](); //Polymorphism
233
 
        } else if(m_class == "document") {
234
 
                g_debug("DOCUMENT (%s)",m_plugin_name.c_str());
235
 
                m_gloobus_interface = factory_document[m_plugin_name.c_str()](); //Polymorphism
236
 
        } else if(m_class == "list") {
237
 
                g_debug("LIST (%s)",m_plugin_name.c_str());
238
 
                m_gloobus_interface = factory_list[m_plugin_name.c_str()](); //Polymorphism
239
 
        } else if(m_class == "movie") {
240
 
                g_debug("MOVIE (%s)",m_plugin_name.c_str());
241
 
                m_gloobus_interface = factory_movie[m_plugin_name.c_str()](); //Polymorphism
242
 
        } else if(m_class == "free") {
243
 
                g_debug("FREE (%s)",m_plugin_name.c_str());
244
 
                m_gloobus_interface = factory_free[m_plugin_name.c_str()](); //Polymorphism
 
55
        m_plugin = PluginManager::get_plugin(get_filetype());
 
56
        if(!m_plugin) {
 
57
                m_class = "default"; // TODO: remove this when iDefault will be made better
 
58
                m_gloobus_interface = new iDefault();
245
59
        } else {
246
 
                g_debug("DEFAULT");
247
 
                m_class = "default";
248
 
                m_gloobus_interface = new iDefault(); //Polymorphism
 
60
                m_class = ""; // TODO: remove this when iDefault will be made better
 
61
                m_gloobus_interface = m_plugin->create();
249
62
        }
250
63
 
251
64
        if(! m_gloobus_interface->set_filename(m_filename))
253
66
        if(! m_gloobus_interface->load() )
254
67
                return false;
255
68
 
256
 
        //FIXME: Do it with signals :
257
 
        //m_timeoutID = g_timeout_add   (200, &Gloobus::check_fullscreen_trick,this);
258
 
        
259
69
        return true;
260
70
}
261
71
// ============================= DESTROY PLUGIN ======================= //
262
72
void Gloobus::destroy_plugin()
263
73
{
264
74
        g_debug("Destroying Plugin:");
265
 
        if(m_timeoutID) {
266
 
                g_source_remove(m_timeoutID);
267
 
                m_timeoutID = 0;
268
 
        }
269
 
        
270
75
        if(m_gloobus_interface) {
271
 
                m_gloobus_interface->end(); //Call a ending function before the destructor
272
 
                                                                         //If we don't do this, thread functions
273
 
                                                                         //calling virtual plugin function may crash
274
 
                if(m_class == "image") {
275
 
                        factoryDestroy_image[m_plugin_name.c_str()](
276
 
                                (iImage*)m_gloobus_interface);
277
 
                } else if(m_class == "music") {
278
 
                        factoryDestroy_music[m_plugin_name.c_str()](
279
 
                                (iMusic*)m_gloobus_interface);
280
 
                } else if(m_class == "movie") {
281
 
                        factoryDestroy_movie[m_plugin_name.c_str()](
282
 
                                (iMovie*)m_gloobus_interface);
283
 
                } else if(m_class == "text") {
284
 
                        factoryDestroy_text[m_plugin_name.c_str()](
285
 
                                (iText*)m_gloobus_interface);
286
 
                } else if(m_class == "document") {
287
 
                        factoryDestroy_document[m_plugin_name.c_str()](
288
 
                                (iDocument*)m_gloobus_interface);
289
 
                } else if(m_class == "list"){
290
 
                        factoryDestroy_list[m_plugin_name.c_str()](
291
 
                                (iList*)m_gloobus_interface);
292
 
                } else if(m_class == "free"){
293
 
                        factoryDestroy_free[m_plugin_name.c_str()](
294
 
                                (iFree*)m_gloobus_interface);
295
 
                } else if(m_class == "default"){
 
76
                if(m_class == "default") { // TODO: remove this when iDefault will be made better
296
77
                        delete m_gloobus_interface;
 
78
                } else {
 
79
                        m_plugin->destroy(m_gloobus_interface);
 
80
                        delete m_plugin;
 
81
                        m_plugin = 0;
297
82
                }
298
83
                m_gloobus_interface = 0;
299
84
        }
300
85
}
301
 
// ============================= GET WIDTH ======================= //
302
 
int Gloobus::get_width(void)
303
 
{
304
 
 
305
 
        return m_gloobus_interface->get_width();
306
 
 
307
 
}
308
 
// ============================= GET HEIGHT ======================= //
309
 
int Gloobus::get_height(void)
310
 
{
311
 
        return m_gloobus_interface->get_height();
312
 
 
313
 
}
 
86
 
314
87
// ============================= DRAW PLUGIN ======================= //
315
88
void Gloobus::draw_plugin(void)
316
89
{
326
99
// ============================= FULLSCREEN WINDOW ======================= //
327
100
void Gloobus::fullscreen (void)
328
101
{
329
 
        m_gloobus_interface->unset_fullscreen_event();
330
 
 
331
102
        GdkScreen *screen = gtk_window_get_screen(GTK_WINDOW(m_gwindow->get_window_pointer()));
332
103
        if(m_gloobus_interface->can_fullscreen())
333
104
        {
334
 
                
335
105
                if(m_fullscreen)        //UNFULLSCREEN
336
106
                {
337
107
                        m_gwindow->set_size                                             (get_width()+SHADOW_WIDTH*2 ,get_height()+SHADOW_WIDTH*2+HEADER_HEIGHT+BOTTOM_HEIGHT);  //Draw in center
341
111
                        gtk_widget_hide_all                                             (m_gwindow->get_toolbar_pointer());
342
112
                        gtk_window_unfullscreen                                 (GTK_WINDOW(m_gwindow->get_window_pointer()));
343
113
                        m_fullscreen = false;
344
 
                }
345
 
                else                            //FULLSCREEN
346
 
                {
 
114
                } else {                        //FULLSCREEN
347
115
                        m_gwindow->set_size                                             (gdk_screen_get_width(screen),gdk_screen_get_height(screen));                   //Draw in center
348
116
                        m_gwindow->hide_common_buttons                  ();                     
349
117
                        m_gwindow->show_toolbar                                 ();
352
120
                        gtk_window_fullscreen                                   (GTK_WINDOW(m_gwindow->get_window_pointer()));
353
121
                        m_fullscreen = true;
354
122
                }
355
 
 
356
123
        }
357
 
        
358
 
 
359
124
}
360
125
// ============================= Load filenames ============================== //
361
126
void Gloobus::load_folder_filenames  ( void )
387
152
                }
388
153
        }while(info!=NULL);
389
154
 
390
 
    g_object_unref(directory);
391
 
    g_object_unref(directory_files);
 
155
        g_object_unref(directory);
 
156
        g_object_unref(directory_files);
392
157
 
393
158
        m_it = m_files.begin();
394
 
    std::sort( m_files.begin(), m_files.end() );
395
 
 
 
159
        std::sort( m_files.begin(), m_files.end() );
396
160
}
397
161
// ============================= Search actual filename ======================= //
398
162
bool Gloobus::search_actual_filename ( void )
399
163
{
400
 
        for ( m_it=m_files.begin() ; m_it < m_files.end(); m_it++ )
401
 
        {
402
 
                //g_debug("%s - %s\n", m_files[i].c_str(), m_filename.c_str());
403
 
                if(*m_it == m_filename)
404
 
                {
 
164
        for ( m_it=m_files.begin() ; m_it < m_files.end() ; m_it++ ) {
 
165
                if(*m_it == m_filename) {
405
166
                         return true;
406
167
                }
407
168
        }
410
171
// ============================= Load Next File ============================== //
411
172
void Gloobus::next_file ( void )
412
173
{
413
 
        //m_current_file++;
414
 
        //if((unsigned int)m_current_file == m_files.size()) m_current_file = 0;
415
 
        if (m_it == m_files.end()-1 or m_it == m_files.end()) 
416
 
        {
417
 
                g_debug("Volta");
 
174
        if (m_it == m_files.end()-1 or m_it == m_files.end()) {
 
175
                g_debug("Going to the start of file list");
418
176
                m_it = m_files.begin();
419
 
        }
420
 
        else
421
 
        {
 
177
        } else {
422
178
                m_it++;
423
179
        }
424
 
        
425
180
        m_filename = *m_it;
426
 
        g_debug("Loading next file %s", m_filename.c_str());
427
 
 
428
 
        end_file_and_new_file();
 
181
        load_preview();
429
182
}
430
183
// =============================== Load prev File ============================ //
431
184
void Gloobus::prev_file ( void )
432
185
{
433
 
        //m_current_file--;
434
 
        //if(m_current_file < 0) m_current_file = m_files.size() - 1;
435
 
        if (m_it == m_files.begin()) 
436
 
        {
437
 
                g_debug("Volta");
 
186
        if (m_it == m_files.begin())  {
 
187
                g_debug("Going to the end of file list");
438
188
                m_it = m_files.end()-1;
439
 
        }
440
 
        else
441
 
        {
 
189
        } else {
442
190
                m_it--;
443
191
        }
444
 
        
445
192
        m_filename = *m_it;
446
 
        g_debug("Loading next file %s", m_filename.c_str());
447
 
        
448
 
        end_file_and_new_file();
 
193
        load_preview();
449
194
}
450
195
// =============================== TRASH FILE ================================ //
451
196
void Gloobus::trash_file( void )
464
209
        }
465
210
        
466
211
        m_filename = *m_it;
467
 
        end_file_and_new_file();
468
 
 
469
 
}
470
 
// ============================= END File and new file ======================= //
471
 
// This functions clears the current window and prepare it for the new file
472
 
// after clearing the window, it draws the new file
473
 
void Gloobus::end_file_and_new_file ( void )
474
 
{
475
 
        destroy_plugin();
476
 
        gtk_widget_destroy      (GTK_WIDGET(m_gwindow->get_container_pointer()));
477
 
        gtk_widget_destroy      (GTK_WIDGET(m_gwindow->get_toolbar_pointer()));
478
 
        if(m_error) {
479
 
                g_error_free(m_error);
480
 
                m_error = 0;
481
 
        }
482
 
 
483
 
        m_gwindow->create_toolbar();
484
 
        g_signal_connect (m_gwindow->get_toolbar_pointer(),"key-press-event",           //And this one, catchs key events
485
 
                                          G_CALLBACK(key_press), NULL);                                                 //to see when space has being pressed, and quit
486
 
 
487
 
        m_gwindow->create_container();                                                  //Create a new container and
488
 
        
489
 
        if(create_plugin()) {
490
 
                m_gwindow->set_size(get_width() +  SHADOW_WIDTH*2, get_height()+ SHADOW_WIDTH*2 + BOTTOM_HEIGHT+HEADER_HEIGHT); 
491
 
                m_gwindow->set_title              (m_filename);
492
 
                m_gwindow->add_common_buttons ();
493
 
                m_gwindow->show_all                       ();
494
 
 
495
 
                draw_plugin();
496
 
 
497
 
                if(m_fullscreen)
498
 
                {
499
 
                        if(m_gloobus_interface->can_fullscreen())
500
 
                        {
501
 
                                m_fullscreen = false;
502
 
                                fullscreen();
503
 
                        }
504
 
                        else
505
 
                        {
506
 
                                gtk_window_unfullscreen         (GTK_WINDOW(m_gwindow->get_window_pointer()));
507
 
                                //m_fullscreen = false;
508
 
                        }
509
 
                }
510
 
        } else {
511
 
                m_error = m_gloobus_interface->get_error();
512
 
                show_error();
513
 
        }
514
 
}
 
212
        load_preview();
 
213
}
 
214
 
515
215
// ============================= Launch default application ======================= //
516
216
// Launch the gnome-open function for the current file
517
217
void Gloobus::launch_default_application ( void )
519
219
        m_gloobus_interface->launch_default_app();
520
220
}
521
221
 
522
 
 
523
 
 
524
222
// ===================================== KEY_PRESS =================================== //
525
223
// This function is called when there is a key press event
526
 
void Gloobus::key_press ( GtkWidget *widget, GdkEventKey *event)
 
224
void Gloobus::key_press_cb ( GtkWidget *widget, GdkEventKey *event)
527
225
{
528
226
    g_message ("Key Pressed %s (%i)",gdk_keyval_name(event->keyval),event->keyval);
529
227
        if(event->keyval == 32 || event->keyval == 65307)               //Space or ESC
531
229
                gtk_main_quit();
532
230
        }
533
231
                
534
 
        if(event->keyval == 102)                                //F key
 
232
        if(event->keyval == GDK_f)
535
233
                Gloobus::instance()->fullscreen();
536
234
                
537
 
        if(event->keyval == 65363)                              //Right Key
 
235
        if(event->keyval == GDK_Right)
538
236
                Gloobus::instance()->next_file();
539
237
 
540
238
        
541
 
        if(event->keyval == 65361)                              //Left key
 
239
        if(event->keyval == GDK_Left)
542
240
                Gloobus::instance()->prev_file();
543
241
                
544
 
        if(event->keyval == 65293){                     //Enter key
 
242
        if(event->keyval == GDK_Return){
545
243
                Gloobus::instance()->launch_default_application();
546
244
                gtk_main_quit();
547
245
        }
548
246
 
549
 
        if(event->keyval == 65535)                              //Delete Key
 
247
        if(event->keyval == GDK_Delete)
550
248
        {
551
249
                Gloobus::instance()->trash_file();
552
250
        }
563
261
        gtk_main_quit();
564
262
}
565
263
 
566
 
gboolean Gloobus::check_fullscreen_trick(gpointer prInstance)                                           
567
 
{
568
 
        Gloobus * instance = (Gloobus*)prInstance;
569
 
        if(instance->m_gloobus_interface->m_fullscreen_event)
570
 
        {       
571
 
                instance->fullscreen( );                
 
264
bool Gloobus::load_file( void ) {
 
265
        if(m_fileinfo)
 
266
                g_object_unref(m_fileinfo);
 
267
        if(m_file)
 
268
                g_object_unref(m_file);
 
269
                
 
270
        m_file = g_file_new_for_path (m_filename.c_str () );
 
271
        m_fileinfo = g_file_query_info(m_file, "*", G_FILE_QUERY_INFO_NONE,
 
272
                NULL, &m_error);
 
273
        if( !m_fileinfo ) {
 
274
                if( !m_error ) {
 
275
                        g_error_new(g_quark_from_string ("Gloobus"), 3,
 
276
                                _("Error loading file info"));
 
277
                }
 
278
                return false;
572
279
        }
573
280
        return true;
574
281
}
575
282
 
576
 
void Gloobus::end_file_and_error( void ) {
 
283
void Gloobus::load_preview( void ) {
 
284
        g_debug("Loading file: %s", m_filename.c_str());
 
285
        destroy_plugin();
 
286
        if(m_error) {
 
287
                g_error_free(m_error);
 
288
                m_error = 0;
 
289
        }
 
290
        if(load_file()) {
 
291
                if(create_plugin()) {
 
292
                        m_gwindow->prepare_window();
 
293
                        g_signal_connect (m_gwindow->get_toolbar_pointer(),
 
294
                                "key-press-event", G_CALLBACK(key_press_cb), NULL);
 
295
 
 
296
                        // TODO: make here draw window only once fullscreen or nonfullscreen
 
297
                        draw_plugin();
 
298
                        if(m_fullscreen) {
 
299
                                if(m_gloobus_interface->can_fullscreen()) {
 
300
                                        m_fullscreen = false;
 
301
                                        fullscreen();
 
302
                                } else {
 
303
                                        gtk_window_unfullscreen         (GTK_WINDOW(m_gwindow->get_window_pointer()));
 
304
                                        //m_fullscreen = false;
 
305
                                }
 
306
                        }
 
307
                } else {
 
308
                        m_error = m_gloobus_interface->get_error();
 
309
                        show_error();
 
310
                }
 
311
        } else {
 
312
                show_error();
 
313
        }
 
314
}
 
315
 
 
316
/* ****************************** GET SECTION ******************************* */
 
317
 
 
318
GloobusConfig* Gloobus::get_config( void ) {
 
319
        if( !m_config ) {
 
320
                m_config = new GloobusConfig;
 
321
                m_config->load_config();
 
322
        }
 
323
        return m_config;
 
324
}
 
325
 
 
326
GFile* Gloobus::get_file( void ) {
 
327
        return m_file;
 
328
}
 
329
 
 
330
GFileInfo* Gloobus::get_fileinfo( void ) {
 
331
        return m_fileinfo;
 
332
}
 
333
 
 
334
string Gloobus::get_filetype( void ) {
 
335
        string type = g_file_info_get_attribute_string (m_fileinfo,
 
336
                "standard::content-type" );
 
337
        type = type.substr(0,type.find(";"));
 
338
        type = type.substr(0,type.find(","));
 
339
        if(type == "x-directory/normal")
 
340
                type = "folder";
 
341
        return type;
 
342
}
 
343
 
 
344
string Gloobus::get_filename( void ) {
 
345
        return m_filename;
 
346
}
 
347
 
 
348
int Gloobus::get_width(void) {
 
349
        return m_gloobus_interface->get_width();
 
350
}
 
351
 
 
352
int Gloobus::get_height(void) {
 
353
        return m_gloobus_interface->get_height();
 
354
}
 
355
 
 
356
/* ****************************** SHOW SECTION ****************************** */
 
357
 
 
358
void Gloobus::show_error( void ) {
 
359
        if(m_gloobus_interface ) {
 
360
                m_error = m_gloobus_interface->get_error();
 
361
                //g_warning(m_error->message);
 
362
                g_debug("Loading iDefault");
 
363
                destroy_plugin();
 
364
                
 
365
                m_class = "default";
 
366
                m_gloobus_interface = new iDefault();
 
367
                m_gloobus_interface->set_filename(m_filename);
 
368
                m_gloobus_interface->load();
 
369
 
 
370
                m_gwindow->prepare_window();
 
371
                g_signal_connect (m_gwindow->get_toolbar_pointer(),
 
372
                        "key-press-event", G_CALLBACK(key_press_cb), NULL);
 
373
                
 
374
                draw_plugin();
 
375
        } else {
 
376
                //g_warning(m_error->message);
 
377
                g_debug("Loading error dialog");
 
378
                gtk_widget_destroy      (GTK_WIDGET(m_gwindow->get_container_pointer()));
 
379
                gtk_widget_destroy      (GTK_WIDGET(m_gwindow->get_toolbar_pointer()));
 
380
 
 
381
                m_gwindow->create_toolbar();
 
382
                m_gwindow->create_container();
 
383
                
 
384
                m_gwindow->set_size(
 
385
                        384 + SHADOW_WIDTH * 2,
 
386
                        128 + SHADOW_WIDTH * 2 /*+ BOTTOM_HEIGHT*/ + HEADER_HEIGHT);
 
387
                m_gwindow->set_title(m_filename);
 
388
                m_gwindow->add_common_buttons();
 
389
                //GtkWidget * box = gtk_vbox_new(false, 0);
 
390
                GtkWidget * icon = gtk_image_new_from_stock(GTK_STOCK_DIALOG_ERROR,
 
391
                        GTK_ICON_SIZE_DIALOG);
 
392
                GtkWidget * label = gtk_label_new(m_error->message);
 
393
                
 
394
                PangoAttrList *info_attrs = pango_attr_list_new();
 
395
                pango_attr_list_insert(info_attrs,
 
396
                        pango_attr_foreground_new(53889,14927,253));
 
397
                pango_attr_list_insert(info_attrs,
 
398
                        pango_attr_weight_new(PANGO_WEIGHT_BOLD));
 
399
                pango_attr_list_insert(info_attrs, pango_attr_size_new(14*PANGO_SCALE));
 
400
                gtk_widget_set_size_request(label, 256, 128);
 
401
                gtk_label_set_attributes(GTK_LABEL(label), info_attrs);
 
402
                gtk_label_set_line_wrap(GTK_LABEL(label), true);
 
403
                gtk_misc_set_alignment(GTK_MISC(label), 0 , 0.5);
 
404
                
 
405
                // GTK_ICON_SIZE_DIALOG is 40, for all themes?
 
406
                // 128-40 /2 = 44 - place icon in the middle
 
407
                gtk_fixed_put(GTK_FIXED(m_gwindow->get_container_pointer()),
 
408
                        icon, SHADOW_WIDTH+44, HEADER_HEIGHT + SHADOW_WIDTH + 44);
 
409
                gtk_fixed_put(GTK_FIXED(m_gwindow->get_container_pointer()),
 
410
                        label, SHADOW_WIDTH+128, HEADER_HEIGHT + SHADOW_WIDTH);
 
411
                gtk_widget_show_all(icon);
 
412
                gtk_widget_show_all(label);
 
413
                m_gwindow->show_all();
 
414
        }
 
415
}
 
416
 
 
417
void Gloobus::show_help  ( )
 
418
{
 
419
        printf(
 
420
                "%s:\n"
 
421
                "   gloobus-preview [%s...] [URI]\n\n",
 
422
                _("Usage"), _("OPTION") );
 
423
 
 
424
        printf(
 
425
                "   %s\n\n", _(
 
426
                "If URI is not provided Gloobus-Preview checks clipboard "
 
427
                "for filename"));
 
428
 
 
429
        printf("%s:\n", _("Help options") );
 
430
        printf("   -h, --help                   %s\n",
 
431
                _("Show help options") );
 
432
        
 
433
        printf("%s:\n", _("Application options") );
 
434
        printf("   -d, --debug                  %s\n",
 
435
                _("Show debug messages") );
 
436
        printf("   --show-supported-filetypes   %s\n",
 
437
                _("Show supported filetypes") );
 
438
 
 
439
        printf("\n");
 
440
}
 
441
 
 
442
void Gloobus::show_preview(char * target)
 
443
{
 
444
        string filename;
 
445
        gloobusWin * win = new gloobusWin();
 
446
        win->create_window( DEFAULT_WIDTH, DEFAULT_HEIGHT );
 
447
        
 
448
        //Events Handlers
 
449
        g_signal_connect (win->get_window_pointer(), "key-press-event",
 
450
                G_CALLBACK(key_press_cb), NULL);
 
451
 
 
452
        win->create_toolbar();
 
453
        g_signal_connect (win->get_toolbar_pointer(),"key-press-event",
 
454
                                          G_CALLBACK(key_press_cb), NULL);
 
455
                
 
456
        if(target) {
 
457
                filename = target;
 
458
 
 
459
                // check relative path:
 
460
                if (filename.c_str()[0] != '/') {
 
461
                        filename = "./" + filename;
 
462
                }
 
463
                
 
464
                // strip final slash:
 
465
                if (filename.c_str()[filename.size()-1] == '/') {
 
466
                        filename = filename.substr(0,filename.size()-1);
 
467
                }
 
468
        } else {
 
469
                g_debug("Getting file from clipboard");
 
470
                GtkClipboard *clip      = gtk_clipboard_get ( GDK_SELECTION_CLIPBOARD );
 
471
                gchar **uri             = gtk_clipboard_wait_for_uris ( clip );
 
472
                if(uri == NULL) {\
 
473
                        m_error = g_error_new(g_quark_from_string ("Gloobus"), 1,
 
474
                                _("There is no file in clipboard"));
 
475
                } else {
 
476
                        filename = g_filename_from_uri(uri[0], NULL, NULL);
 
477
                }
 
478
        }
 
479
        init( filename , win );
 
480
        load_folder_filenames();
 
481
        if( !m_error ) {
 
482
                g_debug("Filename: %s", filename.c_str());
 
483
                if( !search_actual_filename()) {
 
484
                        m_error = g_error_new(g_quark_from_string ("Gloobus"), 2,
 
485
                                _("File not found"));
 
486
                } else {
 
487
                        load_preview();
 
488
                }
 
489
        }
577
490
        if(m_error)
578
 
                g_error_free(m_error);
579
 
        m_error = m_gloobus_interface->get_error();
580
 
        
581
 
        destroy_plugin();
582
 
        gtk_widget_destroy      (GTK_WIDGET(m_gwindow->get_container_pointer()));
583
 
        gtk_widget_destroy      (GTK_WIDGET(m_gwindow->get_toolbar_pointer()));
584
 
 
585
 
        m_gwindow->create_toolbar();
586
 
        m_gwindow->create_container();
587
 
        g_signal_connect (m_gwindow->get_toolbar_pointer(),"key-press-event",           //And this one, catchs key events
588
 
                                          G_CALLBACK(key_press), NULL);
589
 
 
590
 
        show_error(1);
 
491
                show_error();
 
492
  
 
493
    gtk_main ();
 
494
        destroy_plugin(); //Destroy current plugin // TODO: do we really need this?
 
495
        end(); //Unload All plugins before leave // TODO: do we really need this?
 
496
}
 
497
 
 
498
void Gloobus::show_supported_filetypes  ( )
 
499
{
 
500
        PluginManager::load_plugins     ();
 
501
        PluginManager::show_supported_filetypes();
 
502
        PluginManager::unload_plugins();
591
503
}