image_list.c 43.7 KB
Newer Older
1
/*
Stephan Arts's avatar
Stephan Arts committed
2
 *  Copyright (c) Stephan Arts 2006-2012 <stephan@xfce.org>
Stephan Arts's avatar
Stephan Arts committed
3
 *
Stephan Arts's avatar
Stephan Arts committed
4 5 6 7 8
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 * 
Stephan Arts's avatar
Stephan Arts committed
9
 *  This program is distributed in the hope that it will be useful,
10 11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Stephan Arts's avatar
Stephan Arts committed
12 13
 *  GNU General Public License for more details.
 * 
14 15
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
Stephan Arts's avatar
Stephan Arts committed
16 17
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 *  02110-1301, USA.
Stephan Arts's avatar
Stephan Arts committed
18
 *
Stephan Arts's avatar
Stephan Arts committed
19 20
 *  Sorting-algorithm taken from the thunar filemanager. 
 *    Copyright (c) Benedict Meurer <benny@xfce.org>
21 22 23 24
 */

#include <config.h>

25
#include <stdlib.h>
26 27 28 29
#include <string.h>

#include <libexif/exif-data.h>

Stephan Arts's avatar
Stephan Arts committed
30
#include "util.h"
31
#include "image_list.h"
32
#include "thumbnailer.h"
33
#include "settings.h"
34

Stephan Arts's avatar
Stephan Arts committed
35 36 37 38 39
static void
rstto_image_list_tree_model_init (GtkTreeModelIface *iface);
static void
rstto_image_list_tree_sortable_init(GtkTreeSortableIface *iface);

40 41 42 43 44 45 46
static void 
rstto_image_list_init(RsttoImageList *);
static void
rstto_image_list_class_init(RsttoImageListClass *);
static void
rstto_image_list_dispose(GObject *object);

Stephan Arts's avatar
Stephan Arts committed
47 48 49 50 51 52 53 54
static void
cb_file_monitor_changed (
        GFileMonitor      *monitor,
        GFile             *file,
        GFile             *other_file,
        GFileMonitorEvent  event_type,
        gpointer           user_data );

55 56 57 58 59 60
static void 
rstto_image_list_iter_init(RsttoImageListIter *);
static void
rstto_image_list_iter_class_init(RsttoImageListIterClass *);
static void
rstto_image_list_iter_dispose(GObject *object);
Igor's avatar
Igor committed
61 62 63 64
static RsttoImageListIter *
rstto_image_list_iter_new (
        RsttoImageList *nav,
        RsttoFile *r_file);
65

66 67 68 69 70 71
static void
cb_rstto_wrap_images_changed (
        GObject *settings,
        GParamSpec *pspec,
        gpointer user_data);

72 73 74 75 76 77
static void
cb_rstto_thumbnailer_ready(
        RsttoThumbnailer *thumbnailer,
        RsttoFile *file,
        gpointer user_data);

78 79 80 81 82 83 84 85 86
static void
rstto_image_list_monitor_dir (
        RsttoImageList *image_list,
        GFile *dir );

static void
rstto_image_list_remove_all (
        RsttoImageList *image_list);

87 88 89 90
static gboolean
iter_next (
        RsttoImageListIter *iter,
        gboolean sticky);
91

92 93 94 95
static gboolean
iter_previous (
        RsttoImageListIter *iter,
        gboolean sticky);
96

97 98 99 100 101 102
static void 
iter_set_position (
        RsttoImageListIter *iter,
        gint pos,
        gboolean sticky);

Stephan Arts's avatar
Stephan Arts committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
/***************************************/
/*  Begin TreeModelIface Functions     */
/***************************************/

static GtkTreeModelFlags
image_list_model_get_flags ( GtkTreeModel *tree_model );

static gint
image_list_model_get_n_columns ( GtkTreeModel *tree_model );

static GType
image_list_model_get_column_type (
        GtkTreeModel *tree_model,
        gint index_ );

static gboolean
image_list_model_get_iter (
        GtkTreeModel *tree_model,
        GtkTreeIter *iter,
        GtkTreePath *path );

static GtkTreePath *
image_list_model_get_path (
        GtkTreeModel *tree_model,
        GtkTreeIter *iter );

static void 
image_list_model_get_value (
        GtkTreeModel *tree_model,
        GtkTreeIter *iter,
        gint column,
        GValue *value );

static gboolean
image_list_model_iter_children (
        GtkTreeModel *tree_model,
        GtkTreeIter *iter,
        GtkTreeIter *parent );

static gboolean
image_list_model_iter_has_child (
        GtkTreeModel *tree_model,
        GtkTreeIter *iter );

static gboolean
image_list_model_iter_parent (
        GtkTreeModel *tree_model,
        GtkTreeIter *iter,
        GtkTreeIter *child );

static gint
image_list_model_iter_n_children (
        GtkTreeModel *tree_model,
        GtkTreeIter *iter );

static gboolean 
image_list_model_iter_nth_child (
        GtkTreeModel *tree_model,
        GtkTreeIter *iter,
        GtkTreeIter *parent,
        gint n );

static gboolean
image_list_model_iter_next (
        GtkTreeModel *tree_model,
        GtkTreeIter *iter );

/***************************************/
/*  End TreeModelIface Functions       */
/***************************************/

174
static gint
Stephan Arts's avatar
Stephan Arts committed
175
cb_rstto_image_list_image_name_compare_func (RsttoFile *a, RsttoFile *b);
Stephan Arts's avatar
Stephan Arts committed
176
static gint
Igor's avatar
Igor committed
177 178
cb_rstto_image_list_image_type_compare_func (RsttoFile *a, RsttoFile *b);
static gint
Stephan Arts's avatar
Stephan Arts committed
179
cb_rstto_image_list_exif_date_compare_func (RsttoFile *a, RsttoFile *b);
180 181 182 183 184 185

static GObjectClass *parent_class = NULL;
static GObjectClass *iter_parent_class = NULL;

enum
{
Stephan Arts's avatar
Stephan Arts committed
186
    RSTTO_IMAGE_LIST_SIGNAL_REMOVE_IMAGE = 0,
187 188 189 190 191 192 193
    RSTTO_IMAGE_LIST_SIGNAL_REMOVE_ALL,
    RSTTO_IMAGE_LIST_SIGNAL_COUNT
};

enum
{
    RSTTO_IMAGE_LIST_ITER_SIGNAL_CHANGED = 0,
194
    RSTTO_IMAGE_LIST_ITER_SIGNAL_PREPARE_CHANGE,
195 196 197 198 199 200
    RSTTO_IMAGE_LIST_ITER_SIGNAL_COUNT
};

struct _RsttoImageListIterPriv
{
    RsttoImageList *image_list;
Stephan Arts's avatar
Stephan Arts committed
201
    RsttoFile      *r_file;
202 203 204
    
    /* This is set if the iter-position is chosen by the user */
    gboolean        sticky; 
205 206 207 208
};

struct _RsttoImageListPriv
{
Stephan Arts's avatar
Stephan Arts committed
209
    gint           stamp;
210
    GFileMonitor  *dir_monitor;
211
    gint           directory_loader;
212
    RsttoSettings *settings;
213
    RsttoThumbnailer *thumbnailer;
214
    GtkFileFilter *filter;
Stephan Arts's avatar
Stephan Arts committed
215

216
    GList        *image_monitors;
Stephan Arts's avatar
Stephan Arts committed
217 218 219 220 221
    GList        *images;
    gint          n_images;

    GSList       *iterators;
    GCompareFunc  cb_rstto_image_list_compare_func;
222 223

    gboolean      wrap_images;
224 225
};

226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
typedef struct _RsttoFileLoader RsttoFileLoader;

struct _RsttoFileLoader
{
    GFile           *dir;
    RsttoImageList  *image_list;
    GFileEnumerator *file_enum;

    guint            n_files;
    RsttoFile      **files;
};

static gboolean
cb_rstto_read_file ( gpointer user_data );

241 242 243 244
static gint rstto_image_list_signals[RSTTO_IMAGE_LIST_SIGNAL_COUNT];
static gint rstto_image_list_iter_signals[RSTTO_IMAGE_LIST_ITER_SIGNAL_COUNT];

GType
Stephan Arts's avatar
Stephan Arts committed
245
rstto_image_list_get_type (void)
246
{
Stephan Arts's avatar
Stephan Arts committed
247 248 249 250 251 252 253 254 255 256 257 258
    static const GInterfaceInfo tree_model_info =
    {
        (GInterfaceInitFunc) rstto_image_list_tree_model_init,
            NULL,
            NULL
    };
    static const GInterfaceInfo tree_sort_info =
    {
        (GInterfaceInitFunc) rstto_image_list_tree_sortable_init,
            NULL,
            NULL
    };
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
    static GType rstto_image_list_type = 0;

    if (!rstto_image_list_type)
    {
        static const GTypeInfo rstto_image_list_info = 
        {
            sizeof (RsttoImageListClass),
            (GBaseInitFunc) NULL,
            (GBaseFinalizeFunc) NULL,
            (GClassInitFunc) rstto_image_list_class_init,
            (GClassFinalizeFunc) NULL,
            NULL,
            sizeof (RsttoImageList),
            0,
            (GInstanceInitFunc) rstto_image_list_init,
            NULL
        };

        rstto_image_list_type = g_type_register_static (G_TYPE_OBJECT, "RsttoImageList", &rstto_image_list_info, 0);
Stephan Arts's avatar
Stephan Arts committed
278 279 280 281

        g_type_add_interface_static (rstto_image_list_type, GTK_TYPE_TREE_MODEL, &tree_model_info);

        g_type_add_interface_static (rstto_image_list_type, GTK_TYPE_TREE_SORTABLE, &tree_sort_info);
282
    }
Stephan Arts's avatar
Stephan Arts committed
283 284


285 286 287
    return rstto_image_list_type;
}

Stephan Arts's avatar
Stephan Arts committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
static void
rstto_image_list_tree_model_init (GtkTreeModelIface *iface)
{
    iface->get_flags       = image_list_model_get_flags;
    iface->get_n_columns   = image_list_model_get_n_columns;
    iface->get_column_type = image_list_model_get_column_type;
    iface->get_iter        = image_list_model_get_iter;
    iface->get_path        = image_list_model_get_path;
    iface->get_value       = image_list_model_get_value;
    iface->iter_children   = image_list_model_iter_children;
    iface->iter_has_child  = image_list_model_iter_has_child;
    iface->iter_n_children = image_list_model_iter_n_children;
    iface->iter_nth_child  = image_list_model_iter_nth_child;
    iface->iter_parent     = image_list_model_iter_parent;
    iface->iter_next       = image_list_model_iter_next;
}

static void
rstto_image_list_tree_sortable_init(GtkTreeSortableIface *iface)
{
#if 0 
    iface->get_sort_column_id    = sq_archive_store_get_sort_column_id;
    iface->set_sort_column_id    = sq_archive_store_set_sort_column_id;
    iface->set_sort_func         = sq_archive_store_set_sort_func;            /*NOT SUPPORTED*/
    iface->set_default_sort_func = sq_archive_store_set_default_sort_func;    /*NOT SUPPORTED*/
    iface->has_default_sort_func = sq_archive_store_has_default_sort_func;
#endif 
}

317 318 319
static void
rstto_image_list_init(RsttoImageList *image_list)
{
320

321
    image_list->priv = g_new0 (RsttoImageListPriv, 1);
Stephan Arts's avatar
Stephan Arts committed
322
    image_list->priv->stamp = g_random_int();
323
    image_list->priv->settings = rstto_settings_new ();
324
    image_list->priv->thumbnailer = rstto_thumbnailer_new();
325 326 327
    image_list->priv->filter = gtk_file_filter_new ();
    g_object_ref_sink (image_list->priv->filter);
    gtk_file_filter_add_pixbuf_formats (image_list->priv->filter);
Igor's avatar
Igor committed
328 329
    /* see https://bugs.launchpad.net/ubuntu/+source/ristretto/+bug/1778695 */
    gtk_file_filter_add_mime_type (image_list->priv->filter, "image/x-canon-cr2");
330

Stephan Arts's avatar
Stephan Arts committed
331
    image_list->priv->cb_rstto_image_list_compare_func = (GCompareFunc)cb_rstto_image_list_image_name_compare_func;
332 333 334 335 336 337 338 339 340 341 342

    image_list->priv->wrap_images = rstto_settings_get_boolean_property (
            image_list->priv->settings,
            "wrap-images");

    g_signal_connect (
            G_OBJECT(image_list->priv->settings),
            "notify::wrap-images",
            G_CALLBACK (cb_rstto_wrap_images_changed),
            image_list);

343 344 345 346 347 348
    g_signal_connect (
            G_OBJECT(image_list->priv->thumbnailer),
            "ready",
            G_CALLBACK (cb_rstto_thumbnailer_ready),
            image_list);

349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
}

static void
rstto_image_list_class_init(RsttoImageListClass *nav_class)
{
    GObjectClass *object_class = G_OBJECT_CLASS(nav_class);

    parent_class = g_type_class_peek_parent(nav_class);

    object_class->dispose = rstto_image_list_dispose;

    rstto_image_list_signals[RSTTO_IMAGE_LIST_SIGNAL_REMOVE_IMAGE] = g_signal_new("remove-image",
            G_TYPE_FROM_CLASS(nav_class),
            G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
            0,
            NULL,
            NULL,
            g_cclosure_marshal_VOID__OBJECT,
            G_TYPE_NONE,
            1,
            G_TYPE_OBJECT,
            NULL);

    rstto_image_list_signals[RSTTO_IMAGE_LIST_SIGNAL_REMOVE_ALL] = g_signal_new("remove-all",
            G_TYPE_FROM_CLASS(nav_class),
            G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
            0,
            NULL,
            NULL,
            g_cclosure_marshal_VOID__VOID,
            G_TYPE_NONE,
            0,
            NULL);
}

static void
rstto_image_list_dispose(GObject *object)
{
387
    RsttoImageList *image_list = RSTTO_IMAGE_LIST(object);
388

389 390 391 392 393 394 395
    if (NULL != image_list->priv)
    {
        if (image_list->priv->settings)
        {
            g_object_unref (image_list->priv->settings);
            image_list->priv->settings = NULL;
        }
396

397 398 399 400 401 402
        if (image_list->priv->thumbnailer)
        {
            g_object_unref (image_list->priv->thumbnailer);
            image_list->priv->thumbnailer = NULL;
        }

403 404 405 406 407
        if (image_list->priv->filter)
        {
            g_object_unref (image_list->priv->filter);
            image_list->priv->filter= NULL;
        }
Igor's avatar
Igor committed
408 409 410 411 412 413 414 415 416 417 418 419 420

        if (image_list->priv->image_monitors)
        {
            g_list_free_full (image_list->priv->image_monitors, (GDestroyNotify) g_object_unref);
            image_list->priv->image_monitors = NULL;
        }

        if (image_list->priv->images)
        {
            g_list_free_full (image_list->priv->images, (GDestroyNotify) g_object_unref);
            image_list->priv->images = NULL;
        }

421 422 423
        g_free (image_list->priv);
        image_list->priv = NULL;
    }
424 425 426
}

RsttoImageList *
Stephan Arts's avatar
Stephan Arts committed
427
rstto_image_list_new (void)
428 429 430 431 432 433 434 435 436
{
    RsttoImageList *image_list;

    image_list = g_object_new(RSTTO_TYPE_IMAGE_LIST, NULL);

    return image_list;
}

gboolean
437 438
rstto_image_list_add_file (
        RsttoImageList *image_list,
Stephan Arts's avatar
Stephan Arts committed
439
        RsttoFile *r_file,
440
        GError **error )
441
{
442
    GtkFileFilterInfo filter_info;
Stephan Arts's avatar
Stephan Arts committed
443
    GList *image_iter = g_list_find (image_list->priv->images, r_file);
444
    GSList *iter = image_list->priv->iterators;
Stephan Arts's avatar
Stephan Arts committed
445 446 447
    gint i = 0;
    GtkTreePath *path = NULL;
    GtkTreeIter t_iter;
448
    GFileMonitor *monitor = NULL;
449

Stephan Arts's avatar
Stephan Arts committed
450 451
    g_return_val_if_fail ( NULL != r_file , FALSE);
    g_return_val_if_fail ( RSTTO_IS_FILE (r_file) , FALSE);
452

Stephan Arts's avatar
Stephan Arts committed
453 454
    if (!image_iter)
    {
Stephan Arts's avatar
Stephan Arts committed
455
        if (r_file)
Stephan Arts's avatar
Stephan Arts committed
456
        {
457
            filter_info.contains =  GTK_FILE_FILTER_MIME_TYPE | GTK_FILE_FILTER_URI;
Stephan Arts's avatar
Stephan Arts committed
458 459
            filter_info.uri = rstto_file_get_uri (r_file);
            filter_info.mime_type = rstto_file_get_content_type (r_file);
Stephan Arts's avatar
Stephan Arts committed
460

461
            if ( TRUE == gtk_file_filter_filter (image_list->priv->filter, &filter_info))
Stephan Arts's avatar
Stephan Arts committed
462
            {
Stephan Arts's avatar
Stephan Arts committed
463
                g_object_ref (G_OBJECT (r_file));
464

Stephan Arts's avatar
Stephan Arts committed
465 466 467 468
                image_list->priv->images = g_list_insert_sorted (
                        image_list->priv->images,
                        r_file,
                        rstto_image_list_get_compare_func (image_list));
469 470

                image_list->priv->n_images++;
471 472 473 474

                if (image_list->priv->dir_monitor == NULL)
                {
                    monitor = g_file_monitor_file (
Stephan Arts's avatar
Stephan Arts committed
475
                            rstto_file_get_file (r_file),
476 477 478 479 480 481 482 483 484 485 486 487
                            G_FILE_MONITOR_NONE,
                            NULL,
                            NULL);
                    g_signal_connect (
                            G_OBJECT(monitor),
                            "changed",
                            G_CALLBACK (cb_file_monitor_changed),
                            image_list);
                    image_list->priv->image_monitors = g_list_prepend (
                            image_list->priv->image_monitors, 
                            monitor);
                }
Stephan Arts's avatar
Stephan Arts committed
488
                i = g_list_index (image_list->priv->images, r_file);
Stephan Arts's avatar
Stephan Arts committed
489 490 491 492

                path = gtk_tree_path_new();
                gtk_tree_path_append_index (path, i);
                t_iter.stamp = image_list->priv->stamp;
Stephan Arts's avatar
Stephan Arts committed
493
                t_iter.user_data = r_file;
Stephan Arts's avatar
Stephan Arts committed
494 495
                t_iter.user_data3 = GINT_TO_POINTER(i);

Stephan Arts's avatar
Stephan Arts committed
496 497 498 499
                gtk_tree_model_row_inserted (
                        GTK_TREE_MODEL(image_list),
                        path,
                        &t_iter);
500

Marc Schink's avatar
Marc Schink committed
501 502
                gtk_tree_path_free (path);

503 504
                /** TODO: update all iterators */
                while (iter)
Stephan Arts's avatar
Stephan Arts committed
505
                {
506 507
                    if (FALSE == RSTTO_IMAGE_LIST_ITER(iter->data)->priv->sticky)
                    {
Stephan Arts's avatar
Stephan Arts committed
508
                        rstto_image_list_iter_find_file (iter->data, r_file);
509 510
                    }
                    iter = g_slist_next (iter);
Stephan Arts's avatar
Stephan Arts committed
511
                }
Stephan Arts's avatar
Stephan Arts committed
512

513 514 515 516 517
                return TRUE;
            }
            else
            {
                return FALSE;
Stephan Arts's avatar
Stephan Arts committed
518 519
            }
        }
Stephan Arts's avatar
Stephan Arts committed
520
        return FALSE;
521
    }
522

Stephan Arts's avatar
Stephan Arts committed
523
    return TRUE;
524 525 526 527 528 529 530 531
}

gint
rstto_image_list_get_n_images (RsttoImageList *image_list)
{
    return g_list_length (image_list->priv->images);
}

Stephan Arts's avatar
Stephan Arts committed
532 533 534 535 536 537 538 539
/**
 * rstto_image_list_get_iter:
 * @image_list:
 *
 * TODO: track iterators
 *
 * return iter;
 */
540 541 542
RsttoImageListIter *
rstto_image_list_get_iter (RsttoImageList *image_list)
{
Stephan Arts's avatar
Stephan Arts committed
543
    RsttoFile *r_file = NULL;
Stephan Arts's avatar
Stephan Arts committed
544
    RsttoImageListIter *iter = NULL;
Stephan Arts's avatar
Stephan Arts committed
545

546
    if (image_list->priv->images)
Stephan Arts's avatar
Stephan Arts committed
547 548 549
    {
        r_file = image_list->priv->images->data;
    }
550

Stephan Arts's avatar
Stephan Arts committed
551
    iter = rstto_image_list_iter_new (image_list, r_file);
552

Stephan Arts's avatar
Stephan Arts committed
553 554
    image_list->priv->iterators = g_slist_prepend (image_list->priv->iterators, iter);

555 556 557 558 559
    return iter;
}


void
Stephan Arts's avatar
Stephan Arts committed
560 561 562
rstto_image_list_remove_file (
        RsttoImageList *image_list,
        RsttoFile *r_file)
563
{
Stephan Arts's avatar
Stephan Arts committed
564
    GSList *iter = NULL;
Stephan Arts's avatar
Stephan Arts committed
565
    RsttoFile *r_file_a = NULL;
Stephan Arts's avatar
Stephan Arts committed
566
    GtkTreePath *path_ = NULL;
Stephan Arts's avatar
Stephan Arts committed
567 568
    gint index_ = g_list_index (image_list->priv->images, r_file);
    gint n_images = rstto_image_list_get_n_images (image_list);
Stephan Arts's avatar
Stephan Arts committed
569

570
    if (index_ != -1)
571
    {
Stephan Arts's avatar
Stephan Arts committed
572

Stephan Arts's avatar
Stephan Arts committed
573
        iter = image_list->priv->iterators;
Stephan Arts's avatar
Stephan Arts committed
574 575
        while (iter)
        {
Stephan Arts's avatar
Stephan Arts committed
576
            if (rstto_file_equal(rstto_image_list_iter_get_file (iter->data), r_file))
Stephan Arts's avatar
Stephan Arts committed
577
            {
Stephan Arts's avatar
Stephan Arts committed
578
                
Stephan Arts's avatar
Stephan Arts committed
579
                if (rstto_image_list_iter_get_position (iter->data) == n_images -1)
580
                {
581
                    iter_previous (iter->data, FALSE);
582 583 584
                }
                else
                {
585
                    iter_next (iter->data, FALSE);
586
                }
587 588 589 590
                /* If the image is still the same, 
                 * it's a single item list,
                 * and we should force the image in this iter to NULL
                 */
Stephan Arts's avatar
Stephan Arts committed
591 592 593
                if (rstto_file_equal (
                        rstto_image_list_iter_get_file (iter->data),
                        r_file ) )
594
                {
595 596

                    image_list->priv->images = g_list_remove (image_list->priv->images, r_file);
Stephan Arts's avatar
Stephan Arts committed
597 598 599 600 601 602
                    ((RsttoImageListIter *)(iter->data))->priv->r_file = NULL;
                    g_signal_emit (
                            G_OBJECT (iter->data),
                            rstto_image_list_iter_signals[RSTTO_IMAGE_LIST_ITER_SIGNAL_CHANGED],
                            0,
                            NULL);
603
                }
Stephan Arts's avatar
Stephan Arts committed
604 605 606
            }
            iter = g_slist_next (iter);
        }
607

608 609
        image_list->priv->images = g_list_remove (image_list->priv->images, r_file);

610
        path_ = gtk_tree_path_new();
611
        gtk_tree_path_append_index(path_,index_);
612 613 614

        gtk_tree_model_row_deleted(GTK_TREE_MODEL(image_list), path_);

615 616 617
        iter = image_list->priv->iterators;
        while (iter)
        {
Stephan Arts's avatar
Stephan Arts committed
618 619
            r_file_a = rstto_image_list_iter_get_file(iter->data);
            if (NULL != r_file_a)
620
            {
Stephan Arts's avatar
Stephan Arts committed
621
                if (rstto_file_equal(r_file_a, r_file))
622
                {
623
                    iter_next (iter->data, FALSE);
624
                }
625 626 627 628
            }
            iter = g_slist_next (iter);
        }

Stephan Arts's avatar
Stephan Arts committed
629 630 631 632 633 634 635
        g_signal_emit (
                G_OBJECT (image_list),
                rstto_image_list_signals[RSTTO_IMAGE_LIST_SIGNAL_REMOVE_IMAGE],
                0,
                r_file,
                NULL);
        g_object_unref (r_file);
636 637 638
    }
}

639
static void
640 641
rstto_image_list_remove_all (RsttoImageList *image_list)
{
Stephan Arts's avatar
Stephan Arts committed
642
    GSList *iter = NULL;
Stephan Arts's avatar
Stephan Arts committed
643 644 645 646 647 648 649 650 651 652 653
    GList *image_iter = image_list->priv->images;
    GtkTreePath *path_ = NULL;
    gint i = g_list_length (image_iter);

    while (image_iter)
    {
        i--;
        path_ = gtk_tree_path_new();
        gtk_tree_path_append_index(path_, i);

        gtk_tree_model_row_deleted(GTK_TREE_MODEL(image_list), path_);
Igor's avatar
Igor committed
654
        gtk_tree_path_free (path_);
Stephan Arts's avatar
Stephan Arts committed
655

Igor's avatar
Igor committed
656
        image_iter = g_list_next (image_iter);
Stephan Arts's avatar
Stephan Arts committed
657 658
    }

Igor's avatar
Igor committed
659
    g_list_free_full (image_list->priv->image_monitors, (GDestroyNotify) g_object_unref);
660 661
    image_list->priv->image_monitors = NULL;

Igor's avatar
Igor committed
662
    g_list_free_full (image_list->priv->images, (GDestroyNotify) g_object_unref);
663
    image_list->priv->images = NULL;
Stephan Arts's avatar
Stephan Arts committed
664

Stephan Arts's avatar
Stephan Arts committed
665
    iter = image_list->priv->iterators;
Stephan Arts's avatar
Stephan Arts committed
666 667
    while (iter)
    {
668
        iter_set_position (iter->data, -1, FALSE);
Stephan Arts's avatar
Stephan Arts committed
669 670 671
        iter = g_slist_next (iter);
    }
    g_signal_emit (G_OBJECT (image_list), rstto_image_list_signals[RSTTO_IMAGE_LIST_SIGNAL_REMOVE_ALL], 0, NULL);
672 673
}

674 675 676 677 678 679 680 681
gboolean
rstto_image_list_set_directory (
        RsttoImageList *image_list,
        GFile *dir,
        GError **error )
{
    /* Declare variables */
    GFileEnumerator *file_enumerator = NULL;
682
    RsttoFileLoader *loader = NULL;
683 684

    /* Source code block */
685 686 687 688 689 690
    if (image_list->priv->directory_loader != 0)
    {
        g_source_remove (image_list->priv->directory_loader);
        image_list->priv->directory_loader = 0;
    }

691 692
    rstto_image_list_remove_all (image_list);

Stephan Arts's avatar
Stephan Arts committed
693 694 695 696
    rstto_image_list_monitor_dir ( 
            image_list,
            dir );

697 698 699 700 701 702 703
    /* Allow all images to be removed by providing NULL to dir */
    if ( NULL != dir )
    {
        file_enumerator = g_file_enumerate_children (dir, "standard::*", 0, NULL, NULL);

        if (NULL != file_enumerator)
        {
704 705 706 707 708 709 710
            g_object_ref (dir);

            loader = g_new0 (RsttoFileLoader, 1);
            loader->dir = dir;
            loader->file_enum = file_enumerator;
            loader->image_list = image_list;

711
            image_list->priv->directory_loader = g_idle_add ( (GSourceFunc) cb_rstto_read_file, loader );
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
        }
    }

    return TRUE;
}

static gboolean
cb_rstto_read_file ( gpointer user_data )
{
    RsttoFileLoader *loader = user_data;
    GFileInfo       *file_info;
    const gchar     *content_type;
    const gchar     *filename;
    RsttoFile      **files;
    GFile           *child_file;
    guint            i;
    GSList          *iter;

    /* Check the inputs */
    g_return_val_if_fail ( NULL != loader, FALSE );
    g_return_val_if_fail ( NULL != loader->file_enum, FALSE );

    file_info = g_file_enumerator_next_file (
            loader->file_enum,
            NULL,
            NULL );
    if ( NULL != file_info )
    {
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
        /* Allow for 'progressive' loading */
        if (loader->n_files == 100)
        {
            for (i = 0; i < loader->n_files; ++i)
            {
                rstto_image_list_add_file (
                        loader->image_list,
                        loader->files[i],
                        NULL);

                g_object_unref (loader->files[i]);
            }

            iter = loader->image_list->priv->iterators;
            while (iter)
            {
                g_signal_emit (G_OBJECT (iter->data), rstto_image_list_iter_signals[RSTTO_IMAGE_LIST_ITER_SIGNAL_CHANGED], 0, NULL);
                iter = g_slist_next (iter);
            }

            g_free (loader->files);
            loader->files = NULL;
            loader->n_files = 0;
        }

        /* Add file to the list */
766 767 768 769 770 771 772 773
        content_type  = g_file_info_get_content_type (file_info);
        if (strncmp (content_type, "image/", 6) == 0)
        {
            filename = g_file_info_get_name (file_info);
            child_file = g_file_get_child (loader->dir, filename);
            files = g_new0 ( RsttoFile *, loader->n_files+1);
            files[0] = rstto_file_new (child_file);

Marc Schink's avatar
Marc Schink committed
774 775
            g_object_unref (child_file);

776
            for (i = 0; i < loader->n_files; ++i)
777
            {
778
                files[i+1] = loader->files[i];
779
            }
780 781 782 783 784 785 786

            if ( NULL != loader->files )
            {
                g_free (loader->files);
            }
            loader->files = files;
            loader->n_files++;
787
        }
Marc Schink's avatar
Marc Schink committed
788 789

        g_object_unref (file_info);
790
    }
791 792 793 794 795 796 797 798
    else
    {
        for (i = 0; i < loader->n_files; ++i)
        {
            rstto_image_list_add_file (
                    loader->image_list,
                    loader->files[i],
                    NULL);
799

800 801 802
            g_object_unref (loader->files[i]);
        }

Stephan Arts's avatar
Stephan Arts committed
803 804 805 806 807 808 809
        /* This is a hack, use a closure */
        if (loader->image_list->priv->directory_loader != 0)
        {
            g_source_remove (loader->image_list->priv->directory_loader);
            loader->image_list->priv->directory_loader = 0;
        }

810 811 812 813 814 815 816
        iter = loader->image_list->priv->iterators;
        while (iter)
        {
            g_signal_emit (G_OBJECT (iter->data), rstto_image_list_iter_signals[RSTTO_IMAGE_LIST_ITER_SIGNAL_CHANGED], 0, NULL);
            iter = g_slist_next (iter);
        }

Stephan Arts's avatar
Stephan Arts committed
817 818 819 820 821 822 823
        if (loader->n_files > 0)
        {
            g_free (loader->files);
        }
        g_object_unref (loader->file_enum);
        g_free (loader);

824 825
        return FALSE;
    }
826 827 828 829
    return TRUE;
}

static void
Stephan Arts's avatar
Stephan Arts committed
830 831 832 833 834 835
rstto_image_list_monitor_dir (
        RsttoImageList *image_list,
        GFile *dir )
{
    GFileMonitor *monitor = NULL;

836
    if ( NULL != image_list->priv->dir_monitor )
Stephan Arts's avatar
Stephan Arts committed
837
    {
838 839
        g_object_unref (image_list->priv->dir_monitor);
        image_list->priv->dir_monitor = NULL;
Stephan Arts's avatar
Stephan Arts committed
840 841
    }

842 843 844 845 846 847 848 849 850
    /* Allow a monitor to be removed by providing NULL to dir */
    if ( NULL != dir )
    {
        monitor = g_file_monitor_directory (
                dir,
                G_FILE_MONITOR_NONE,
                NULL,
                NULL);

851 852 853 854 855 856 857 858
        if ( monitor != NULL )
        {
            g_signal_connect (
                    G_OBJECT (monitor),
                    "changed",
                    G_CALLBACK (cb_file_monitor_changed),
                    image_list);
        }
859
    }
Stephan Arts's avatar
Stephan Arts committed
860

861 862
    if (image_list->priv->image_monitors)
    {
Igor's avatar
Igor committed
863
        g_list_free_full (image_list->priv->image_monitors, (GDestroyNotify) g_object_unref);
864 865 866 867
        image_list->priv->image_monitors = NULL;
    }

    image_list->priv->dir_monitor = monitor;
Stephan Arts's avatar
Stephan Arts committed
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
}

static void
cb_file_monitor_changed (
        GFileMonitor      *monitor,
        GFile             *file,
        GFile             *other_file,
        GFileMonitorEvent  event_type,
        gpointer           user_data )
{
    RsttoImageList *image_list = RSTTO_IMAGE_LIST (user_data);
    RsttoFile *r_file = rstto_file_new (file);

    switch ( event_type )
    {
        case G_FILE_MONITOR_EVENT_DELETED:
            rstto_image_list_remove_file ( image_list, r_file );
885 886 887 888 889 890 891 892
            if (image_list->priv->dir_monitor == NULL)
            {
                image_list->priv->image_monitors = g_list_remove (
                        image_list->priv->image_monitors,
                        monitor);   
                g_object_unref (monitor);
                monitor = NULL;
            }
Stephan Arts's avatar
Stephan Arts committed
893 894 895 896 897 898
            break;
        case G_FILE_MONITOR_EVENT_CREATED:
            rstto_image_list_add_file (image_list, r_file, NULL);
            break;
        case G_FILE_MONITOR_EVENT_MOVED:
            rstto_image_list_remove_file ( image_list, r_file );
899 900 901 902

            /* Remove our reference, reusing pointer */
            g_object_unref (r_file);

Stephan Arts's avatar
Stephan Arts committed
903 904
            r_file = rstto_file_new (other_file);
            rstto_image_list_add_file (image_list, r_file, NULL);
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925

            if (image_list->priv->dir_monitor == NULL)
            {
                image_list->priv->image_monitors = g_list_remove (
                        image_list->priv->image_monitors,
                        monitor);   
                g_object_unref (monitor);
                monitor = g_file_monitor_file (
                        other_file,
                        G_FILE_MONITOR_NONE,
                        NULL,
                        NULL);
                g_signal_connect (
                        G_OBJECT(monitor),
                        "changed",
                        G_CALLBACK (cb_file_monitor_changed),
                        image_list);
                image_list->priv->image_monitors = g_list_prepend (
                        image_list->priv->image_monitors, 
                        monitor);
            }
Stephan Arts's avatar
Stephan Arts committed
926
            break;
Stephan Arts's avatar
Stephan Arts committed
927 928 929
        case G_FILE_MONITOR_EVENT_CHANGED:
            rstto_file_changed (r_file);
            break;
Stephan Arts's avatar
Stephan Arts committed
930 931 932 933 934 935 936 937 938 939
        default:
            break;
    }

    if ( NULL != r_file )
    {
        g_object_unref (r_file);
    }
}

940 941 942


GType
Stephan Arts's avatar
Stephan Arts committed
943
rstto_image_list_iter_get_type (void)
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
{
    static GType rstto_image_list_iter_type = 0;

    if (!rstto_image_list_iter_type)
    {
        static const GTypeInfo rstto_image_list_iter_info = 
        {
            sizeof (RsttoImageListIterClass),
            (GBaseInitFunc) NULL,
            (GBaseFinalizeFunc) NULL,
            (GClassInitFunc) rstto_image_list_iter_class_init,
            (GClassFinalizeFunc) NULL,
            NULL,
            sizeof (RsttoImageListIter),
            0,
            (GInstanceInitFunc) rstto_image_list_iter_init,
            NULL
        };

        rstto_image_list_iter_type = g_type_register_static (G_TYPE_OBJECT, "RsttoImageListIter", &rstto_image_list_iter_info, 0);
    }
    return rstto_image_list_iter_type;
}

static void
rstto_image_list_iter_init (RsttoImageListIter *iter)
{
    iter->priv = g_new0 (RsttoImageListIterPriv, 1);
}

static void
rstto_image_list_iter_class_init(RsttoImageListIterClass *iter_class)
{
    GObjectClass *object_class = G_OBJECT_CLASS(iter_class);

    iter_parent_class = g_type_class_peek_parent(iter_class);

    object_class->dispose = rstto_image_list_iter_dispose;

983 984 985 986 987 988 989 990 991 992 993
    rstto_image_list_iter_signals[RSTTO_IMAGE_LIST_ITER_SIGNAL_PREPARE_CHANGE] = g_signal_new("prepare-change",
            G_TYPE_FROM_CLASS(iter_class),
            G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
            0,
            NULL,
            NULL,
            g_cclosure_marshal_VOID__VOID,
            G_TYPE_NONE,
            0,
            NULL);

994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
    rstto_image_list_iter_signals[RSTTO_IMAGE_LIST_ITER_SIGNAL_CHANGED] = g_signal_new("changed",
            G_TYPE_FROM_CLASS(iter_class),
            G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
            0,
            NULL,
            NULL,
            g_cclosure_marshal_VOID__VOID,
            G_TYPE_NONE,
            0,
            NULL);
}

static void
rstto_image_list_iter_dispose (GObject *object)
{
    RsttoImageListIter *iter = RSTTO_IMAGE_LIST_ITER(object);
Stephan Arts's avatar
Stephan Arts committed
1010

Marc Schink's avatar
Marc Schink committed
1011
    if (iter->priv)
Stephan Arts's avatar
Stephan Arts committed
1012
    {
Marc Schink's avatar
Marc Schink committed
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
        if (iter->priv->r_file)
        {
            iter->priv->r_file = NULL;
        }

        if (iter->priv->image_list)
        {
            iter->priv->image_list->priv->iterators = g_slist_remove (iter->priv->image_list->priv->iterators, iter);
            iter->priv->image_list= NULL;
        }

        g_free (iter->priv);
        iter->priv = NULL;
Stephan Arts's avatar
Stephan Arts committed
1026
    }
1027 1028 1029
}

static RsttoImageListIter *
Stephan Arts's avatar
Stephan Arts committed
1030 1031 1032
rstto_image_list_iter_new (
        RsttoImageList *nav,
        RsttoFile *r_file)
1033 1034 1035 1036
{
    RsttoImageListIter *iter;

    iter = g_object_new(RSTTO_TYPE_IMAGE_LIST_ITER, NULL);
Stephan Arts's avatar
Stephan Arts committed
1037
    iter->priv->r_file = r_file;
1038 1039 1040 1041 1042 1043
    iter->priv->image_list = nav;

    return iter;
}

gboolean
Stephan Arts's avatar
Stephan Arts committed
1044 1045 1046
rstto_image_list_iter_find_file (
        RsttoImageListIter *iter,
        RsttoFile *r_file)
1047
{
Stephan Arts's avatar
Stephan Arts committed
1048 1049
    gint pos = g_list_index (iter->priv->image_list->priv->images, r_file);

1050 1051
    if (pos > -1)
    {
Stephan Arts's avatar
Stephan Arts committed
1052 1053 1054 1055 1056
        g_signal_emit (
                G_OBJECT (iter),
                rstto_image_list_iter_signals[RSTTO_IMAGE_LIST_ITER_SIGNAL_PREPARE_CHANGE],
                0,
                NULL);
1057

Stephan Arts's avatar
Stephan Arts committed
1058
        if (iter->priv->r_file)
1059
        {
Stephan Arts's avatar
Stephan Arts committed
1060
            iter->priv->r_file = NULL;
1061
        }
Stephan Arts's avatar
Stephan Arts committed
1062
        iter->priv->r_file = r_file;
1063
        iter->priv->sticky = TRUE;
1064

Stephan Arts's avatar
Stephan Arts committed
1065 1066 1067 1068 1069
        g_signal_emit (
                G_OBJECT (iter),
                rstto_image_list_iter_signals[RSTTO_IMAGE_LIST_ITER_SIGNAL_CHANGED],
                0,
                NULL);
1070 1071 1072 1073 1074 1075 1076 1077 1078

        return TRUE;
    }
    return FALSE;
}

gint
rstto_image_list_iter_get_position (RsttoImageListIter *iter)
{
Stephan Arts's avatar
Stephan Arts committed
1079
    if ( NULL == iter->priv->r_file )
1080
    {
1081
        return -1;
1082
    }
Stephan Arts's avatar
Stephan Arts committed
1083
    return g_list_index (iter->priv->image_list->priv->images, iter->priv->r_file);
1084 1085
}

Stephan Arts's avatar
Stephan Arts committed
1086
RsttoFile *
1087
rstto_image_list_iter_get_file (RsttoImageListIter *iter)
1088
{
Stephan Arts's avatar
Stephan Arts committed
1089
    return iter->priv->r_file;
1090 1091
}

1092 1093 1094 1095 1096
static void
iter_set_position (
        RsttoImageListIter *iter,
        gint pos,
        gboolean sticky )
1097
{
Stephan Arts's avatar
Stephan Arts committed
1098 1099 1100 1101 1102
    g_signal_emit (
            G_OBJECT (iter),
            rstto_image_list_iter_signals[RSTTO_IMAGE_LIST_ITER_SIGNAL_PREPARE_CHANGE],
            0,
            NULL);
1103

Stephan Arts's avatar
Stephan Arts committed
1104
    if (iter->priv->r_file)
1105
    {
Stephan Arts's avatar
Stephan Arts committed
1106
        iter->priv->r_file = NULL;
1107 1108
    }

Stephan Arts's avatar
Stephan Arts committed
1109
    if (pos >= 0)
Stephan Arts's avatar
Stephan Arts committed
1110
    {
Stephan Arts's avatar
Stephan Arts committed
1111
        iter->priv->r_file = g_list_nth_data (iter->priv->image_list->priv->images, pos); 
Stephan Arts's avatar
Stephan Arts committed
1112
    }
1113

Stephan Arts's avatar
Stephan Arts committed
1114 1115 1116 1117 1118
    g_signal_emit (
            G_OBJECT (iter),
            rstto_image_list_iter_signals[RSTTO_IMAGE_LIST_ITER_SIGNAL_CHANGED],
            0,
            NULL);
1119 1120
}

1121
void
Stephan Arts's avatar
Stephan Arts committed
1122 1123 1124
rstto_image_list_iter_set_position (
        RsttoImageListIter *iter,
        gint pos)
1125 1126 1127 1128 1129 1130 1131 1132
{
    iter_set_position ( iter, pos, TRUE );
}

static gboolean
iter_next (
        RsttoImageListIter *iter,
        gboolean sticky)
1133
{
1134
    GList *position = NULL;
1135
    RsttoImageList *image_list = iter->priv->image_list;
Stephan Arts's avatar
Stephan Arts committed
1136
    RsttoFile *r_file = iter->priv->r_file;
1137
    gboolean ret_val = FALSE;
1138

Stephan Arts's avatar
Stephan Arts committed
1139 1140 1141 1142 1143
    g_signal_emit (
            G_OBJECT (iter),
            rstto_image_list_iter_signals[RSTTO_IMAGE_LIST_ITER_SIGNAL_PREPARE_CHANGE],
            0,
            NULL);
1144

Stephan Arts's avatar
Stephan Arts committed
1145
    if (r_file)
1146
    {
Stephan Arts's avatar
Stephan Arts committed
1147 1148
        position = g_list_find (iter->priv->image_list->priv->images, r_file);
        iter->priv->r_file = NULL;
1149 1150
    }

1151 1152
    iter->priv->sticky = sticky;

1153 1154
    position = g_list_next (position);
    if (position)
1155
    {
Stephan Arts's avatar
Stephan Arts committed
1156
        iter->priv->r_file = position->data; 
1157 1158 1159 1160

        /* We could move forward, set ret_val to TRUE */
        ret_val = TRUE;
    }
1161 1162
    else
    {
Stephan Arts's avatar
Stephan Arts committed
1163

1164
        if (TRUE == image_list->priv->wrap_images)
1165
        {
1166
            position = g_list_first (iter->priv->image_list->priv->images);
1167 1168 1169 1170 1171 1172

            /* We could move forward, wrapped back to the start of the
             * list, set ret_val to TRUE
             */
            ret_val = TRUE;
        }
1173
        else
Stephan Arts's avatar
Stephan Arts committed
1174
        {
1175
            position = g_list_last (iter->priv->image_list->priv->images);
Stephan Arts's avatar
Stephan Arts committed
1176
        }
1177

1178
        if (position)
Stephan Arts's avatar
Stephan Arts committed
1179 1180 1181
        {
            iter->priv->r_file = position->data; 
        }
1182
        else
Stephan Arts's avatar
Stephan Arts committed
1183 1184 1185
        {
            iter->priv->r_file = NULL;
        }
1186 1187
    }

Stephan Arts's avatar
Stephan Arts committed
1188
    if (r_file != iter->priv->r_file)
1189
    {
Stephan Arts's avatar
Stephan Arts committed
1190 1191 1192 1193 1194
        g_signal_emit (
                G_OBJECT (iter),
                rstto_image_list_iter_signals[RSTTO_IMAGE_LIST_ITER_SIGNAL_CHANGED],
                0,
                NULL);
1195
    }
1196 1197

    return ret_val;
1198 1199
}

1200
gboolean
1201 1202 1203 1204 1205
rstto_image_list_iter_next (RsttoImageListIter *iter)
{
    return iter_next (iter, TRUE);
}

1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225