~ubuntu-branches/ubuntu/wily/gmpc/wily

« back to all changes in this revision

Viewing changes to src/browsers/playlist3-file-browser.c

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Cornet
  • Date: 2010-03-27 15:04:05 UTC
  • mfrom: (1.3.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20100327150405-x9trnrmvg4kxydrr
Tags: 0.20.0-1
* New upstream release
* Remove man-fix-whatis.patch now upstream
* Added build-dep to valac
* Bump standards-version (no change)
* Remove config.log at clean time

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* Gnome Music Player Client (GMPC)
2
 
 * Copyright (C) 2004-2009 Qball Cow <qball@sarine.nl>
 
2
 * Copyright (C) 2004-2010 Qball Cow <qball@sarine.nl>
3
3
 * Project homepage: http://gmpc.wikia.com/
4
4
 
5
5
 * This program is free software; you can redistribute it and/or modify
25
25
#include "misc.h"
26
26
#include "playlist3.h"
27
27
#include "playlist3-file-browser.h"
 
28
#include "playlist3-find2-browser.h"
 
29
#include "advanced-search.h"
28
30
#include "gmpc-mpddata-model.h"
29
31
#include "gmpc-mpddata-treeview.h"
30
32
#include "playlist3-playlist-editor.h"
31
 
#include "gmpc-paned-size-group.h"
 
33
#include "gmpc-extras.h"
 
34
 
 
35
#define LOG_DOMAIN "FileBrowser"
 
36
 
 
37
static void pl3_file_browser_plugin_init(void);
 
38
 
 
39
static gboolean pl3_file_browser_is_field_supported(int tag);
 
40
static MpdData * pl3_file_browser_is_search(const int num_field ,const gchar *search_string, GError **error);
32
41
 
33
42
static void pl3_file_browser_destroy(void);
34
43
static void pl3_file_browser_add(GtkWidget *cat_tree);
92
101
        .selected       = pl3_file_browser_selected,
93
102
        .unselected     = pl3_file_browser_unselected,
94
103
        .add_go_menu    = pl3_file_browser_add_go_menu,
 
104
    .integrate_search_field_supported = pl3_file_browser_is_field_supported,
 
105
    .integrate_search   = pl3_file_browser_is_search
95
106
};
96
107
 
97
108
gmpcPlugin file_browser_plug = {
98
 
        .name =                                                 N_("File Browser"),
 
109
        .name =                                                 N_("Database"),
99
110
        .version =                                              {1,1,1},
100
111
        .plugin_type =                                  GMPC_PLUGIN_PL_BROWSER|GMPC_INTERNALL,
 
112
    .init =                         pl3_file_browser_plugin_init,
101
113
        .destroy =                                              pl3_file_browser_destroy,
102
114
        .browser =                                              &file_browser_gbp,
103
115
        .mpd_status_changed =                   pl3_file_browser_status_changed,
157
169
    sw = gtk_scrolled_window_new(NULL, NULL);
158
170
        gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_AUTOMATIC,GTK_POLICY_AUTOMATIC);
159
171
        gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw), GTK_SHADOW_ETCHED_IN);
160
 
    pl3_fb_dir_tree = tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL(pl3_fb_dir_store));
161
 
    gtk_tree_view_set_enable_tree_lines(GTK_TREE_VIEW(tree), TRUE);
162
 
    gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(tree), TRUE);
 
172
        pl3_fb_dir_tree = tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL(pl3_fb_dir_store));
 
173
        gtk_tree_view_set_enable_tree_lines(GTK_TREE_VIEW(tree), TRUE);
 
174
        gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(tree), TRUE);
163
175
        column = gtk_tree_view_column_new();
164
176
        gtk_tree_view_column_set_title(column, _("Directories"));
165
177
        renderer = gtk_cell_renderer_pixbuf_new();
166
178
        gtk_tree_view_column_pack_start(column, renderer, FALSE);
167
 
    gtk_tree_view_set_fixed_height_mode(GTK_TREE_VIEW(tree), TRUE);
168
 
    gtk_tree_view_column_add_attribute(column, renderer, "icon-name",PL3_FB_ICON);
169
 
    gtk_tree_view_column_set_sizing(column , GTK_TREE_VIEW_COLUMN_FIXED);
 
179
        gtk_tree_view_column_add_attribute(column, renderer, "icon-name",PL3_FB_ICON);
 
180
        gtk_tree_view_column_set_sizing(column , GTK_TREE_VIEW_COLUMN_FIXED);
170
181
        renderer = gtk_cell_renderer_text_new();
171
182
        gtk_tree_view_column_pack_start(column, renderer, TRUE);
172
183
        gtk_tree_view_column_add_attribute(column, renderer, "text", PL3_FB_NAME);
173
184
        gtk_tree_view_insert_column(GTK_TREE_VIEW(tree),column, -1);
174
185
 
175
 
    gtk_tree_view_set_search_column(GTK_TREE_VIEW(pl3_fb_dir_tree), PL3_FB_NAME);
 
186
        gtk_tree_view_set_fixed_height_mode(GTK_TREE_VIEW(tree), TRUE);
 
187
        gtk_tree_view_set_search_column(GTK_TREE_VIEW(pl3_fb_dir_tree), PL3_FB_NAME);
176
188
        /* set the search column */
177
189
        gtk_tree_view_set_search_column(GTK_TREE_VIEW(tree), PL3_FB_NAME);
178
190
 
207
219
        gtk_box_pack_start(GTK_BOX(vbox), pl3_fb_sw, TRUE, TRUE,0);
208
220
        gtk_widget_show_all(pl3_fb_sw); 
209
221
 
210
 
        /* Warning box for when there is no music */
211
 
        pl3_fb_warning_box = gtk_label_new("");
212
 
        gtk_label_set_markup(GTK_LABEL(pl3_fb_warning_box), 
213
 
        _("It seems you have no music in your database.\n"
214
 
          "To add music, copy the music to your <i>music_directory</i> as specified in your mpd config file.\n"
215
 
          "Then update the database. (Server->Update Database)"));
216
 
        gtk_misc_set_alignment(GTK_MISC(pl3_fb_warning_box), 0.0,0.0);
217
 
        gtk_misc_set_padding(GTK_MISC(pl3_fb_warning_box), 12,12);
 
222
    /* Warning box for when there is no music */
 
223
    pl3_fb_warning_box = gtk_label_new("");
 
224
    gtk_label_set_markup(GTK_LABEL(pl3_fb_warning_box), 
 
225
            _("It seems you have no music in your database.\n"
 
226
                "To add music, copy the music to your <i>music_directory</i> as specified in your mpd config file.\n"
 
227
                "Then update the database. (Server->Update Database)"));
 
228
    gtk_misc_set_alignment(GTK_MISC(pl3_fb_warning_box), 0.0,0.0);
 
229
    gtk_misc_set_padding(GTK_MISC(pl3_fb_warning_box), 12,12);
218
230
        gtk_widget_set_no_show_all(pl3_fb_warning_box, TRUE);
219
231
        gtk_box_pack_end(GTK_BOX(vbox), pl3_fb_warning_box, FALSE, TRUE,0);
220
232
 
221
233
    gtk_paned_add2(GTK_PANED(pl3_fb_vbox), vbox);
222
234
        /* set initial state */
223
 
        debug_printf(DEBUG_INFO,"initialized current playlist treeview\n");
224
235
    gtk_widget_show(vbox);
225
236
    gtk_widget_show(pl3_fb_vbox);
226
 
        g_object_ref(G_OBJECT(pl3_fb_vbox));
 
237
        g_object_ref_sink(G_OBJECT(pl3_fb_vbox));
227
238
}
228
239
 
229
240
static void pl3_file_browser_add_folder(void)
332
343
        playlist3_insert_browser(&iter, pos);
333
344
        gtk_list_store_set(GTK_LIST_STORE(pl3_tree), &iter, 
334
345
                        PL3_CAT_TYPE, file_browser_plug.id,
335
 
                        PL3_CAT_TITLE, _("File Browser"),
336
 
                        PL3_CAT_ICON_ID, "gtk-open",
 
346
                        PL3_CAT_TITLE, _("Database"),
 
347
                        PL3_CAT_ICON_ID, "gmpc-database",
337
348
                        -1);
338
349
        /* add fantom child for lazy tree */
339
350
 
370
381
    MpdData_real *a = *(MpdData_real **)ppaa;
371
382
    MpdData_real *b = *(MpdData_real **)ppbb;
372
383
    int val = 0;
373
 
    if(a->type == MPD_DATA_TYPE_DIRECTORY & b->type == MPD_DATA_TYPE_DIRECTORY)
 
384
    if(!(a && b)) return val;
 
385
    if(a->type != b->type) return a->type - b->type;
 
386
    if((a->type == MPD_DATA_TYPE_DIRECTORY) && (b->type == MPD_DATA_TYPE_DIRECTORY))
374
387
    {
375
388
        if(a->directory && b->directory) {
376
389
            gchar *sa,*sb;
383
396
    }
384
397
    return val;
385
398
}
 
399
/* Reverse sort function, needed for prepending instead of appending */
 
400
static int directory_sort_func_inv(gpointer a, gpointer b, gpointer d)
 
401
{
 
402
  return - directory_sort_func (a, b, d);
 
403
}
 
404
 
386
405
static void pl3_file_browser_reupdate_folder(GtkTreeIter *iter)
387
406
{
388
407
        MpdData *data = NULL;
394
413
        {
395
414
                GtkTreeIter child, child2,child3;
396
415
                data = mpd_database_get_directory(connection,path);
397
 
 
398
 
        data = misc_sort_mpddata(data,(GCompareDataFunc)directory_sort_func,NULL); 
399
 
        g_free(path);
 
416
                data = misc_sort_mpddata(data,(GCompareDataFunc)directory_sort_func,NULL); 
 
417
                g_free(path);
400
418
                if(gtk_tree_model_iter_children(model, &child, iter))
401
419
                {
402
420
                        gchar *test_path = NULL;
485
503
 
486
504
                GtkTreeModel *model = GTK_TREE_MODEL(pl3_fb_dir_store); 
487
505
                
488
 
                if(mpd_stats_get_total_songs(connection) == 0)
 
506
                if(mpd_stats_get_total_songs(connection) == 0 && !mpd_status_db_is_updating(connection))
489
507
                {
490
508
                    gtk_widget_show(pl3_fb_warning_box);
491
509
                }else{
528
546
        if(strcmp("media-playlist",icon))
529
547
        {
530
548
                data = mpd_database_get_directory(connection, path);
531
 
        data = misc_sort_mpddata(data,(GCompareDataFunc)directory_sort_func,NULL); 
532
549
        }
533
550
        else{
534
 
                debug_printf(DEBUG_INFO,"View Playlist\n");
535
551
                data = mpd_database_get_playlist_content(connection, path);
536
552
        }
537
553
        /* Check, and set the up arrow in the model */
560
576
    /* add phantom child */
561
577
    gtk_tree_store_append(pl3_fb_dir_store, &child, iter);
562
578
}
563
 
 
564
579
static void pl3_file_browser_fill_tree(GtkWidget *tree,GtkTreeIter *iter, GtkTreePath *tpath, gpointer user_data)
565
580
{
566
581
    char *path;
567
582
    MpdData *data = NULL;
568
 
    GtkTreeIter child,child2;
 
583
    GtkTreeIter child,child2, dummy;
569
584
    gboolean open;
570
585
    gtk_tree_model_get(GTK_TREE_MODEL(pl3_fb_dir_store),iter, PL3_FB_PATH, &path, PL3_FB_OPEN, &open,-1);
571
586
    gtk_tree_store_set(pl3_fb_dir_store, iter, PL3_FB_OPEN, TRUE, -1);
572
587
    if(open == FALSE)
573
588
    {
 
589
        GTimer *tim = g_timer_new();
574
590
        data = mpd_database_get_directory(connection, path);
575
 
        data = misc_sort_mpddata(data,(GCompareDataFunc)directory_sort_func,NULL); 
576
 
        while (data != NULL)
 
591
        /* Do a reverse sort, because adding it to the gtk view by prepending is faster
 
592
         * then appending */
 
593
        data = misc_sort_mpddata(data,(GCompareDataFunc)directory_sort_func_inv,NULL);
 
594
        g_log(LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Elapsed time sorting before adding: %f\n",
 
595
                g_timer_elapsed(tim, NULL));
 
596
 
 
597
        if(gtk_tree_model_iter_children(GTK_TREE_MODEL(pl3_fb_dir_store), &dummy, iter))
577
598
        {
578
 
            if (data->type == MPD_DATA_TYPE_DIRECTORY)
 
599
            while (data != NULL)
579
600
            {
580
 
                gchar *basename =
581
 
                    g_path_get_basename (data->directory);
582
 
                gtk_tree_store_append (pl3_fb_dir_store, &child, iter);
583
 
                gtk_tree_store_set (pl3_fb_dir_store, &child,
584
 
                        PL3_FB_ICON, "gtk-open",
585
 
                        PL3_FB_NAME, basename,
586
 
                        PL3_FB_PATH, data->directory,
587
 
                        PL3_FB_OPEN, FALSE,
588
 
                        -1);
589
 
                gtk_tree_store_append(pl3_fb_dir_store, &child2, &child);
 
601
                if (data->type == MPD_DATA_TYPE_DIRECTORY)
 
602
                {
 
603
                    gchar *basename = g_path_get_basename (data->directory);
 
604
                    gtk_tree_store_prepend(pl3_fb_dir_store, &child, iter);
 
605
                    gtk_tree_store_set (pl3_fb_dir_store, &child,
 
606
                            PL3_FB_ICON, "gtk-open",
 
607
                            PL3_FB_NAME, basename,
 
608
                            PL3_FB_PATH, data->directory,
 
609
                            PL3_FB_OPEN, FALSE,
 
610
                            -1);
 
611
                    gtk_tree_store_append(pl3_fb_dir_store, &child2, &child);
590
612
 
591
 
                q_free (basename);
 
613
                    q_free (basename);
 
614
                }
 
615
                data = mpd_data_get_next(data);
592
616
            }
593
 
            data = mpd_data_get_next(data);
594
 
        }
595
 
        if(gtk_tree_model_iter_children(GTK_TREE_MODEL(pl3_fb_dir_store), &child, iter))
596
 
        {
597
 
            gtk_tree_store_remove(pl3_fb_dir_store, &child);
598
 
        }
 
617
 
 
618
            gtk_tree_store_remove(pl3_fb_dir_store, &dummy);
 
619
        }
 
620
 
 
621
        g_log(LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Elapsed time sorting after adding: %f\n",
 
622
                g_timer_elapsed(tim, NULL));
 
623
        g_timer_destroy(tim);
 
624
 
599
625
    }
 
626
 
600
627
    q_free(path);
601
628
}
602
629
 
624
651
        g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(pl3_file_browser_replace_folder), NULL);
625
652
 
626
653
        {
627
 
            GtkTreeView *tree = GTK_TREE_VIEW(pl3_fb_dir_tree); 
628
 
            GtkTreeModel *model = (GtkTreeModel *)pl3_fb_dir_store; 
 
654
            GtkTreeView *tree = GTK_TREE_VIEW(pl3_fb_dir_tree);
 
655
            GtkTreeModel *model = (GtkTreeModel *)pl3_fb_dir_store;
629
656
            GtkTreeSelection *selection  = gtk_tree_view_get_selection(tree);
630
657
            GtkTreeIter iter;
631
658
            if(gtk_tree_selection_get_selected(selection, &model, &iter))
1190
1217
    if(!pl3_file_browser_get_enabled())
1191
1218
        return 0;
1192
1219
 
1193
 
    item = gtk_image_menu_item_new_with_label(_("File Browser"));
 
1220
    item = gtk_image_menu_item_new_with_label(_("Database"));
1194
1221
    gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), 
1195
 
            gtk_image_new_from_stock("gtk-open", GTK_ICON_SIZE_MENU));
 
1222
            gtk_image_new_from_icon_name("gmpc-database", GTK_ICON_SIZE_MENU));
1196
1223
    gtk_widget_add_accelerator(GTK_WIDGET(item), 
1197
1224
            "activate",
1198
1225
            gtk_menu_get_accel_group(GTK_MENU(menu)), 
1227
1254
 
1228
1255
static void pl3_file_browser_status_changed(MpdObj *mi,ChangedStatusType what, void *data)
1229
1256
{
 
1257
    if(what&MPD_CST_UPDATING) {
 
1258
        if(pl3_fb_vbox != NULL && mpd_status_db_is_updating(connection)){
 
1259
            gtk_widget_hide(pl3_fb_warning_box);
 
1260
        }
 
1261
    }
1230
1262
    if(what&MPD_CST_DATABASE)
1231
1263
    {
1232
1264
        pl3_file_browser_reupdate();
1256
1288
        if(path)
1257
1289
        {
1258
1290
            gint *indices = gtk_tree_path_get_indices(path);
1259
 
            debug_printf(DEBUG_INFO,"Saving myself to position: %i\n", indices[0]);
 
1291
            g_log(LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Saving myself to position: %i\n", indices[0]);
1260
1292
            cfg_set_single_value_as_int(config, "file-browser","position",indices[0]);
1261
1293
            gtk_tree_path_free(path);
1262
1294
        }
1315
1347
        }
1316
1348
    }
1317
1349
}
 
1350
 
 
1351
 
 
1352
static gboolean pl3_file_browser_is_field_supported(int tag)
 
1353
{
 
1354
    if(tag == MPD_TAG_NUM_OF_ITEM_TYPES) return TRUE; 
 
1355
    return mpd_server_tag_supported(connection, tag);
 
1356
}
 
1357
 
 
1358
static MpdData * pl3_file_browser_is_search(const int num_field , const gchar *search_string, GError **error)
 
1359
{
 
1360
    MpdData *data_t = NULL;
 
1361
    if(num_field == MPD_TAG_NUM_OF_ITEM_TYPES){
 
1362
        data_t = advanced_search(search_string, FALSE);
 
1363
    }else{
 
1364
        gchar ** splitted = tokenize_string(search_string);
 
1365
        int i =0;
 
1366
        gboolean found = FALSE;
 
1367
        for(i=0;splitted && splitted[i];i++) {
 
1368
            if(!found) {
 
1369
                mpd_database_search_start(connection, FALSE);
 
1370
                found = TRUE;
 
1371
            }
 
1372
            mpd_database_search_add_constraint(connection, num_field, splitted[i]);
 
1373
        }
 
1374
        if(splitted)
 
1375
            g_strfreev(splitted);
 
1376
        if(found) {
 
1377
            data_t = mpd_database_search_commit(connection);
 
1378
        }
 
1379
    }
 
1380
    return data_t;
 
1381
}
 
1382
 
 
1383
void pl3_find2_ec_database(gpointer user_data, const char *param)
 
1384
{
 
1385
    pl3_find2_select_plugin_id(file_browser_plug.id);
 
1386
    pl3_find2_do_search_any(param);
 
1387
}
 
1388
 
 
1389
static void pl3_file_browser_plugin_init(void)
 
1390
{
 
1391
    gmpc_easy_command_add_entry(gmpc_easy_command, 
 
1392
                _("search database"), ".*",
 
1393
                _("Search database <query>"),
 
1394
                (GmpcEasyCommandCallback *)pl3_find2_ec_database, NULL);
 
1395
}