image_list.c 36.3 KB
Newer Older
1
/*
2
 *  Copyright (c) Stephan Arts 2009-2011 <stephan@xfce.org>
3 4 5 6 7
 *
 *  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
8 9
 *
 *  This program is distributed in the hope that it will be useful,
10 11 12 13 14 15 16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Stephan Arts's avatar
Stephan Arts committed
17
 *
Stephan Arts's avatar
Stephan Arts committed
18 19
 *  Sorting-algorithm taken from the thunar filemanager. 
 *    Copyright (c) Benedict Meurer <benny@xfce.org>
20 21 22 23 24 25 26
 */

#include <config.h>

#include <gtk/gtk.h>
#include <gtk/gtkmarshal.h>

27
#include <stdlib.h>
28 29 30 31
#include <string.h>

#include <libexif/exif-data.h>

Stephan Arts's avatar
Stephan Arts committed
32
#include "util.h"
Stephan Arts's avatar
Stephan Arts committed
33
#include "file.h"
34
#include "image_list.h"
35
#include "settings.h"
36

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

42 43 44 45 46 47 48
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
49 50 51 52 53 54 55 56
static void
cb_file_monitor_changed (
        GFileMonitor      *monitor,
        GFile             *file,
        GFile             *other_file,
        GFileMonitorEvent  event_type,
        gpointer           user_data );

57 58 59 60 61 62 63
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);

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

70 71 72 73 74 75 76 77 78
static void
rstto_image_list_monitor_dir (
        RsttoImageList *image_list,
        GFile *dir );

static void
rstto_image_list_remove_all (
        RsttoImageList *image_list);

79 80 81 82
static gboolean
iter_next (
        RsttoImageListIter *iter,
        gboolean sticky);
83

84 85 86 87
static gboolean
iter_previous (
        RsttoImageListIter *iter,
        gboolean sticky);
88

89 90 91 92 93 94
static void 
iter_set_position (
        RsttoImageListIter *iter,
        gint pos,
        gboolean sticky);

Stephan Arts's avatar
Stephan Arts committed
95 96 97 98 99 100 101 102 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
/***************************************/
/*  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       */
/***************************************/

166 167 168
static RsttoImageListIter * rstto_image_list_iter_new ();

static gint
Stephan Arts's avatar
Stephan Arts committed
169
cb_rstto_image_list_image_name_compare_func (RsttoFile *a, RsttoFile *b);
Stephan Arts's avatar
Stephan Arts committed
170
static gint
Stephan Arts's avatar
Stephan Arts committed
171
cb_rstto_image_list_exif_date_compare_func (RsttoFile *a, RsttoFile *b);
172 173 174 175 176 177

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

enum
{
Stephan Arts's avatar
Stephan Arts committed
178
    RSTTO_IMAGE_LIST_SIGNAL_REMOVE_IMAGE = 0,
179 180 181 182 183 184 185
    RSTTO_IMAGE_LIST_SIGNAL_REMOVE_ALL,
    RSTTO_IMAGE_LIST_SIGNAL_COUNT
};

enum
{
    RSTTO_IMAGE_LIST_ITER_SIGNAL_CHANGED = 0,
186
    RSTTO_IMAGE_LIST_ITER_SIGNAL_PREPARE_CHANGE,
187 188 189
    RSTTO_IMAGE_LIST_ITER_SIGNAL_COUNT
};

Stephan Arts's avatar
Stephan Arts committed
190 191 192 193 194
/* Missing image thumbnail, should be re-generated every time
 * the thumbnail-size changed.
 */
static GdkPixbuf *thumbnail_missing = NULL;

195 196 197
struct _RsttoImageListIterPriv
{
    RsttoImageList *image_list;
198 199 200 201
    RsttoFile      *file;
    
    /* This is set if the iter-position is chosen by the user */
    gboolean        sticky; 
202 203 204 205
};

struct _RsttoImageListPriv
{
Stephan Arts's avatar
Stephan Arts committed
206 207
    gint           stamp;
    GFileMonitor  *monitor;
208
    RsttoSettings *settings;
209
    GtkFileFilter *filter;
Stephan Arts's avatar
Stephan Arts committed
210

Stephan Arts's avatar
Stephan Arts committed
211 212 213 214 215
    GList        *images;
    gint          n_images;

    GSList       *iterators;
    GCompareFunc  cb_rstto_image_list_compare_func;
216 217

    gboolean      wrap_images;
218 219 220 221 222 223
};

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
224
rstto_image_list_get_type (void)
225
{
Stephan Arts's avatar
Stephan Arts committed
226 227 228 229 230 231 232 233 234 235 236 237
    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
    };
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    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
257 258 259 260

        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);
261
    }
Stephan Arts's avatar
Stephan Arts committed
262 263


264 265 266
    return rstto_image_list_type;
}

Stephan Arts's avatar
Stephan Arts committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
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 
}

296 297 298
static void
rstto_image_list_init(RsttoImageList *image_list)
{
299

300
    image_list->priv = g_new0 (RsttoImageListPriv, 1);
Stephan Arts's avatar
Stephan Arts committed
301
    image_list->priv->stamp = g_random_int();
302
    image_list->priv->settings = rstto_settings_new ();
303 304 305
    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);
306

Stephan Arts's avatar
Stephan Arts committed
307
    image_list->priv->cb_rstto_image_list_compare_func = (GCompareFunc)cb_rstto_image_list_image_name_compare_func;
308 309 310 311 312 313 314 315 316 317 318

    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);

319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
}

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)
{
357 358 359 360 361 362 363 364
    RsttoImageList *image_list = RSTTO_IMAGE_LIST(object);
    if (NULL != image_list->priv)
    {
        if (image_list->priv->settings)
        {
            g_object_unref (image_list->priv->settings);
            image_list->priv->settings = NULL;
        }
365 366 367 368 369 370

        if (image_list->priv->filter)
        {
            g_object_unref (image_list->priv->filter);
            image_list->priv->filter= NULL;
        }
371 372 373 374
        
        g_free (image_list->priv);
        image_list->priv = NULL;
    }
375 376 377
}

RsttoImageList *
Stephan Arts's avatar
Stephan Arts committed
378
rstto_image_list_new (void)
379 380 381 382 383 384 385 386 387
{
    RsttoImageList *image_list;

    image_list = g_object_new(RSTTO_TYPE_IMAGE_LIST, NULL);

    return image_list;
}

gboolean
388 389 390 391
rstto_image_list_add_file (
        RsttoImageList *image_list,
        RsttoFile *file,
        GError **error )
392
{
393
    GtkFileFilterInfo filter_info;
Stephan Arts's avatar
Stephan Arts committed
394
    GList *image_iter = g_list_find (image_list->priv->images, file);
395
    GSList *iter = image_list->priv->iterators;
Stephan Arts's avatar
Stephan Arts committed
396 397 398
    gint i = 0;
    GtkTreePath *path = NULL;
    GtkTreeIter t_iter;
399

400 401 402
    g_return_val_if_fail ( NULL != file , FALSE);
    g_return_val_if_fail ( RSTTO_IS_FILE (file) , FALSE);

Stephan Arts's avatar
Stephan Arts committed
403 404
    if (!image_iter)
    {
405
        if (file)
Stephan Arts's avatar
Stephan Arts committed
406
        {
407 408 409
            filter_info.contains =  GTK_FILE_FILTER_MIME_TYPE | GTK_FILE_FILTER_URI;
            filter_info.uri = rstto_file_get_uri (file);
            filter_info.mime_type = rstto_file_get_content_type (file);
Stephan Arts's avatar
Stephan Arts committed
410

411
            if ( TRUE == gtk_file_filter_filter (image_list->priv->filter, &filter_info))
Stephan Arts's avatar
Stephan Arts committed
412
            {
413 414
                g_object_ref (G_OBJECT (file));

415 416 417
                image_list->priv->images = g_list_insert_sorted (image_list->priv->images, file, rstto_image_list_get_compare_func (image_list));

                image_list->priv->n_images++;
Stephan Arts's avatar
Stephan Arts committed
418 419 420 421 422 423 424 425 426
                i = g_list_index (image_list->priv->images, file);

                path = gtk_tree_path_new();
                gtk_tree_path_append_index (path, i);
                t_iter.stamp = image_list->priv->stamp;
                t_iter.user_data = file;
                t_iter.user_data3 = GINT_TO_POINTER(i);

                gtk_tree_model_row_inserted(GTK_TREE_MODEL(image_list), path, &t_iter);
427 428 429

                /** TODO: update all iterators */
                while (iter)
Stephan Arts's avatar
Stephan Arts committed
430
                {
431 432 433 434 435
                    if (FALSE == RSTTO_IMAGE_LIST_ITER(iter->data)->priv->sticky)
                    {
                        rstto_image_list_iter_find_file (iter->data, file);
                    }
                    iter = g_slist_next (iter);
Stephan Arts's avatar
Stephan Arts committed
436
                }
Stephan Arts's avatar
Stephan Arts committed
437

438 439 440 441 442
                return TRUE;
            }
            else
            {
                return FALSE;
Stephan Arts's avatar
Stephan Arts committed
443 444
            }
        }
Stephan Arts's avatar
Stephan Arts committed
445
        return FALSE;
446
    }
447

Stephan Arts's avatar
Stephan Arts committed
448
    return TRUE;
449 450 451 452 453 454 455 456
}

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
457 458 459 460 461 462 463 464
/**
 * rstto_image_list_get_iter:
 * @image_list:
 *
 * TODO: track iterators
 *
 * return iter;
 */
465 466 467
RsttoImageListIter *
rstto_image_list_get_iter (RsttoImageList *image_list)
{
Stephan Arts's avatar
Stephan Arts committed
468
    RsttoFile *file = NULL;
Stephan Arts's avatar
Stephan Arts committed
469
    RsttoImageListIter *iter = NULL;
470
    if (image_list->priv->images)
471
        file = image_list->priv->images->data;
472

473
    iter = rstto_image_list_iter_new (image_list, file);
474

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

477 478 479 480 481
    return iter;
}


void
Stephan Arts's avatar
Stephan Arts committed
482
rstto_image_list_remove_file (RsttoImageList *image_list, RsttoFile *file)
483
{
Stephan Arts's avatar
Stephan Arts committed
484
    GSList *iter = NULL;
Stephan Arts's avatar
Stephan Arts committed
485
    RsttoFile *afile = NULL;
Stephan Arts's avatar
Stephan Arts committed
486
    GtkTreePath *path_ = NULL;
Stephan Arts's avatar
Stephan Arts committed
487

488
    if (g_list_find(image_list->priv->images, file))
489
    {
Stephan Arts's avatar
Stephan Arts committed
490

Stephan Arts's avatar
Stephan Arts committed
491
        iter = image_list->priv->iterators;
Stephan Arts's avatar
Stephan Arts committed
492 493
        while (iter)
        {
Stephan Arts's avatar
Stephan Arts committed
494
            if (rstto_file_equal(rstto_image_list_iter_get_file (iter->data), file))
Stephan Arts's avatar
Stephan Arts committed
495
            {
Stephan Arts's avatar
Stephan Arts committed
496 497 498 499 500 501
                
                path_ = gtk_tree_path_new();
                gtk_tree_path_append_index(path_,rstto_image_list_iter_get_position (iter->data));

                gtk_tree_model_row_deleted(GTK_TREE_MODEL(image_list), path_);

502
                if (rstto_image_list_iter_get_position (iter->data) == rstto_image_list_get_n_images (image_list)-1)
503
                {
504
                    iter_previous (iter->data, FALSE);
505 506 507
                }
                else
                {
508
                    iter_next (iter->data, FALSE);
509
                }
510 511 512 513
                /* 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
514
                if (rstto_file_equal(rstto_image_list_iter_get_file (iter->data), file))
515
                {
516
                    ((RsttoImageListIter *)(iter->data))->priv->file = NULL;
517
                    g_signal_emit (G_OBJECT (iter->data), rstto_image_list_iter_signals[RSTTO_IMAGE_LIST_ITER_SIGNAL_CHANGED], 0, NULL);
518
                }
Stephan Arts's avatar
Stephan Arts committed
519 520 521
            }
            iter = g_slist_next (iter);
        }
522

523
        image_list->priv->images = g_list_remove (image_list->priv->images, file);
524 525 526
        iter = image_list->priv->iterators;
        while (iter)
        {
527 528
            afile = rstto_image_list_iter_get_file(iter->data);
            if (NULL != afile)
529
            {
Stephan Arts's avatar
Stephan Arts committed
530
                if (rstto_file_equal(afile, file))
531
                {
532
                    iter_next (iter->data, FALSE);
533
                }
534 535 536 537
            }
            iter = g_slist_next (iter);
        }

538 539
        g_signal_emit (G_OBJECT (image_list), rstto_image_list_signals[RSTTO_IMAGE_LIST_SIGNAL_REMOVE_IMAGE], 0, file, NULL);
        g_object_unref(file);
540 541 542
    }
}

543
static void
544 545
rstto_image_list_remove_all (RsttoImageList *image_list)
{
Stephan Arts's avatar
Stephan Arts committed
546
    GSList *iter = NULL;
Stephan Arts's avatar
Stephan Arts committed
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
    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_);

        
        image_iter = g_list_next (image_iter);     
    }

563 564 565
    g_list_foreach (image_list->priv->images, (GFunc)g_object_unref, NULL);
    g_list_free (image_list->priv->images);
    image_list->priv->images = NULL;
Stephan Arts's avatar
Stephan Arts committed
566

Stephan Arts's avatar
Stephan Arts committed
567
    iter = image_list->priv->iterators;
Stephan Arts's avatar
Stephan Arts committed
568 569
    while (iter)
    {
570
        iter_set_position (iter->data, -1, FALSE);
Stephan Arts's avatar
Stephan Arts committed
571 572 573
        iter = g_slist_next (iter);
    }
    g_signal_emit (G_OBJECT (image_list), rstto_image_list_signals[RSTTO_IMAGE_LIST_SIGNAL_REMOVE_ALL], 0, NULL);
574 575
}

576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
gboolean
rstto_image_list_set_directory (
        RsttoImageList *image_list,
        GFile *dir,
        GError **error )
{
    /* Declare variables */
    GFileEnumerator *file_enumerator = NULL;
    GFileInfo *file_info;
    const gchar *filename;
    const gchar *content_type;
    GFile *child_file;

    /* Source code block */
    rstto_image_list_remove_all (image_list);

    /* 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)
        {
            for(file_info = g_file_enumerator_next_file (file_enumerator, NULL, NULL);
                NULL != file_info;
                file_info = g_file_enumerator_next_file (file_enumerator, NULL, NULL))
            {
                filename = g_file_info_get_name (file_info);
                content_type  = g_file_info_get_content_type (file_info);
                child_file = g_file_get_child (dir, filename);
                if (strncmp (content_type, "image/", 6) == 0)
                {
                    rstto_image_list_add_file (image_list, rstto_file_new (child_file), NULL);
                }
            }
            g_object_unref (file_enumerator);
            file_enumerator = NULL;
        }
    }

    rstto_image_list_monitor_dir ( image_list, dir );

    return TRUE;
}

static void
Stephan Arts's avatar
Stephan Arts committed
622 623 624 625 626 627 628 629 630 631 632 633
rstto_image_list_monitor_dir (
        RsttoImageList *image_list,
        GFile *dir )
{
    GFileMonitor *monitor = NULL;

    if ( NULL != image_list->priv->monitor )
    {
        g_object_unref (image_list->priv->monitor);
        image_list->priv->monitor = NULL;
    }

634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
    /* 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);

        g_signal_connect (
                G_OBJECT(monitor),
                "changed",
                G_CALLBACK (cb_file_monitor_changed),
                image_list);
    }
Stephan Arts's avatar
Stephan Arts committed
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675

    image_list->priv->monitor = monitor;
}

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);

    g_return_if_fail ( monitor == image_list->priv->monitor);

    switch ( event_type )
    {
        case G_FILE_MONITOR_EVENT_DELETED:
            rstto_image_list_remove_file ( image_list, r_file );
            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 );
676 677 678 679

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

Stephan Arts's avatar
Stephan Arts committed
680 681 682 683 684 685 686 687 688 689 690 691 692
            r_file = rstto_file_new (other_file);
            rstto_image_list_add_file (image_list, r_file, NULL);
            break;
        default:
            break;
    }

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

693 694 695


GType
Stephan Arts's avatar
Stephan Arts committed
696
rstto_image_list_iter_get_type (void)
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
{
    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;

736 737 738 739 740 741 742 743 744 745 746
    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);

747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
    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);
764
    if (iter->priv->file)
765
    {
766
        iter->priv->file = NULL;
767
    }
Stephan Arts's avatar
Stephan Arts committed
768 769 770 771 772 773

    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;
    }
774 775 776
}

static RsttoImageListIter *
Stephan Arts's avatar
Stephan Arts committed
777
rstto_image_list_iter_new (RsttoImageList *nav, RsttoFile *file)
778 779 780 781
{
    RsttoImageListIter *iter;

    iter = g_object_new(RSTTO_TYPE_IMAGE_LIST_ITER, NULL);
782
    iter->priv->file = file;
783 784 785 786 787 788
    iter->priv->image_list = nav;

    return iter;
}

gboolean
Stephan Arts's avatar
Stephan Arts committed
789
rstto_image_list_iter_find_file (RsttoImageListIter *iter, RsttoFile *file)
790
{
791
    gint pos = g_list_index (iter->priv->image_list->priv->images, file);
792 793
    if (pos > -1)
    {
794 795
        g_signal_emit (G_OBJECT (iter), rstto_image_list_iter_signals[RSTTO_IMAGE_LIST_ITER_SIGNAL_PREPARE_CHANGE], 0, NULL);

796
        if (iter->priv->file)
797
        {
798
            iter->priv->file = NULL;
799
        }
800
        iter->priv->file = file;
801
        iter->priv->sticky = TRUE;
802 803 804 805 806 807 808 809 810 811 812

        g_signal_emit (G_OBJECT (iter), rstto_image_list_iter_signals[RSTTO_IMAGE_LIST_ITER_SIGNAL_CHANGED], 0, NULL);

        return TRUE;
    }
    return FALSE;
}

gint
rstto_image_list_iter_get_position (RsttoImageListIter *iter)
{
813
    if ( NULL == iter->priv->file )
814
    {
815
        return -1;
816
    }
817
    return g_list_index (iter->priv->image_list->priv->images, iter->priv->file);
818 819
}

Stephan Arts's avatar
Stephan Arts committed
820
RsttoFile *
821
rstto_image_list_iter_get_file (RsttoImageListIter *iter)
822
{
823
    return iter->priv->file;
824 825
}

826 827 828 829 830
static void
iter_set_position (
        RsttoImageListIter *iter,
        gint pos,
        gboolean sticky )
831
{
832 833
    g_signal_emit (G_OBJECT (iter), rstto_image_list_iter_signals[RSTTO_IMAGE_LIST_ITER_SIGNAL_PREPARE_CHANGE], 0, NULL);

834
    if (iter->priv->file)
835
    {
836
        iter->priv->file = NULL;
837 838
    }

Stephan Arts's avatar
Stephan Arts committed
839
    if (pos >= 0)
Stephan Arts's avatar
Stephan Arts committed
840
    {
841
        iter->priv->file = g_list_nth_data (iter->priv->image_list->priv->images, pos); 
Stephan Arts's avatar
Stephan Arts committed
842
    }
843

844 845 846
    g_signal_emit (G_OBJECT (iter), rstto_image_list_iter_signals[RSTTO_IMAGE_LIST_ITER_SIGNAL_CHANGED], 0, NULL);
}

847 848 849 850 851 852 853 854 855 856
void
rstto_image_list_iter_set_position (RsttoImageListIter *iter, gint pos)
{
    iter_set_position ( iter, pos, TRUE );
}

static gboolean
iter_next (
        RsttoImageListIter *iter,
        gboolean sticky)
857
{
858
    GList *position = NULL;
859
    RsttoImageList *image_list = iter->priv->image_list;
860
    RsttoFile *file = iter->priv->file;
861
    gboolean ret_val = FALSE;
862 863 864

    g_signal_emit (G_OBJECT (iter), rstto_image_list_iter_signals[RSTTO_IMAGE_LIST_ITER_SIGNAL_PREPARE_CHANGE], 0, NULL);

865
    if (iter->priv->file)
866
    {
867 868
        position = g_list_find (iter->priv->image_list->priv->images, iter->priv->file);
        iter->priv->file = NULL;
869 870
    }

871 872
    iter->priv->sticky = sticky;

873 874
    position = g_list_next (position);
    if (position)
875
    {
876
        iter->priv->file = position->data; 
877 878 879 880

        /* We could move forward, set ret_val to TRUE */
        ret_val = TRUE;
    }
881 882
    else
    {
Stephan Arts's avatar
Stephan Arts committed
883

884
        if (TRUE == image_list->priv->wrap_images)
885
        {
886
            position = g_list_first (iter->priv->image_list->priv->images);
887 888 889 890 891 892

            /* We could move forward, wrapped back to the start of the
             * list, set ret_val to TRUE
             */
            ret_val = TRUE;
        }
893 894 895
        else
            position = g_list_last (iter->priv->image_list->priv->images);

896
        if (position)
897
            iter->priv->file = position->data; 
898
        else
899
            iter->priv->file = NULL;
900 901
    }

902 903 904 905
    if (file != iter->priv->file)
    {
        g_signal_emit (G_OBJECT (iter), rstto_image_list_iter_signals[RSTTO_IMAGE_LIST_ITER_SIGNAL_CHANGED], 0, NULL);
    }
906 907

    return ret_val;
908 909
}

910
gboolean
911 912 913 914 915
rstto_image_list_iter_next (RsttoImageListIter *iter)
{
    return iter_next (iter, TRUE);
}

916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
gboolean
rstto_image_list_iter_has_next (RsttoImageListIter *iter)
{
    RsttoImageList *image_list = iter->priv->image_list;

    if (image_list->priv->wrap_images)
    {
        return TRUE;
    }
    else
    {
        if (rstto_image_list_iter_get_position (iter) ==
            (rstto_image_list_get_n_images (image_list) -1))
        {
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }
}

939 940 941 942
static gboolean
iter_previous (
        RsttoImageListIter *iter,
        gboolean sticky)
943
{
944
    GList *position = NULL;
945
    RsttoImageList *image_list = iter->priv->image_list;
946
    RsttoFile *file = iter->priv->file;
947
    gboolean ret_val = FALSE;
948 949 950

    g_signal_emit (G_OBJECT (iter), rstto_image_list_iter_signals[RSTTO_IMAGE_LIST_ITER_SIGNAL_PREPARE_CHANGE], 0, NULL);

951
    if (iter->priv->file)
952
    {
953 954
        position = g_list_find (iter->priv->image_list->priv->images, iter->priv->file);
        iter->priv->file = NULL;
955 956
    }

957 958
    iter->priv->sticky = sticky;

959 960
    position = g_list_previous (position);
    if (position)
961
    {
962
        iter->priv->file = position->data; 
963
    }
964 965
    else
    {
966 967
        if (TRUE == image_list->priv->wrap_images)
        {
968
            position = g_list_last (iter->priv->image_list->priv->images);
969
        }
970
        else
971
        {
972
            position = g_list_first (iter->priv->image_list->priv->images);
973
        }
974

975
        if (position)
976
        {
977
            iter->priv->file = position->data; 
978
        }
979
        else
980
        {
981
            iter->priv->file = NULL;
982
        }
983
    }
984

985 986 987 988
    if (file != iter->priv->file)
    {
        g_signal_emit (G_OBJECT (iter), rstto_image_list_iter_signals[RSTTO_IMAGE_LIST_ITER_SIGNAL_CHANGED], 0, NULL);
    }
989 990

    return ret_val;
991 992 993 994 995 996

}
gboolean
rstto_image_list_iter_previous (RsttoImageListIter *iter)
{
    return iter_previous (iter, TRUE);
997 998
}

999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022

gboolean
rstto_image_list_iter_has_previous (RsttoImageListIter *iter)
{
    RsttoImageList *image_list = iter->priv->image_list;

    if (image_list->priv->wrap_images)
    {
        return TRUE;
    }
    else
    {
        if (rstto_image_list_iter_get_position (iter) == 0)
        {
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }
}


1023 1024 1025
RsttoImageListIter *
rstto_image_list_iter_clone (RsttoImageListIter *iter)
{
1026
    RsttoImageListIter *new_iter = rstto_image_list_iter_new (iter->priv->image_list, iter->priv->file);
Stephan Arts's avatar
Stephan Arts committed
1027
    rstto_image_list_iter_set_position (new_iter, rstto_image_list_iter_get_position(iter));
1028 1029 1030

    return new_iter;
}
1031 1032 1033 1034

GCompareFunc
rstto_image_list_get_compare_func (RsttoImageList *image_list)
{
Stephan Arts's avatar
Stephan Arts committed
1035 1036 1037
    return (GCompareFunc)image_list->priv->cb_rstto_image_list_compare_func;
}

1038 1039 1040
void
rstto_image_list_set_compare_func (RsttoImageList *image_list, GCompareFunc func)
{
1041
    GSList *iter = NULL;
1042
    image_list->priv->cb_rstto_image_list_compare_func = func;
1043
    image_list->priv->images = g_list_sort (image_list->priv->images,  func);
1044

1045 1046 1047 1048
    for (iter = image_list->priv->iterators; iter != NULL; iter = g_slist_next (iter))
    {
        g_signal_emit (G_OBJECT (iter->data), rstto_image_list_iter_signals[RSTTO_IMAGE_LIST_ITER_SIGNAL_CHANGED], 0, NULL);
    }
1049 1050
}

Stephan Arts's avatar
Stephan Arts committed
1051 1052 1053 1054
/***********************/
/*  Compare Functions  */
/***********************/

1055 1056 1057
void
rstto_image_list_set_sort_by_name (RsttoImageList *image_list)
{
1058
    rstto_image_list_set_compare_func (image_list, (GCompareFunc)cb_rstto_image_list_image_name_compare_func);
1059 1060 1061 1062 1063
}

void
rstto_image_list_set_sort_by_date (RsttoImageList *image_list)
{
1064
    rstto_image_list_set_compare_func (image_list, (GCompareFunc)cb_rstto_image_list_exif_date_compare_func);
1065 1066
}

Stephan Arts's avatar
Stephan Arts committed
1067 1068 1069 1070 1071 1072 1073 1074 1075
/**
 * cb_rstto_image_list_image_name_compare_func:
 * @a:
 * @b:
 *
 *
 * Return value: (see strcmp)
 */
static gint
Stephan Arts's avatar
Stephan Arts committed
1076
cb_rstto_image_list_image_name_compare_func (RsttoFile *a, RsttoFile *b)
Stephan Arts's avatar
Stephan Arts committed
1077
{
Stephan Arts's avatar
Stephan Arts committed
1078 1079
    const gchar *a_base = rstto_file_get_display_name (a);
    const gchar *b_base = rstto_file_get_display_name (b);
1080 1081 1082 1083 1084
    guint  ac;
    guint  bc;
    const gchar *ap = a_base;
    const gchar *bp = b_base;

Stephan Arts's avatar
Stephan Arts committed
1085
    gint result = 0;
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
    guint a_num = 0;
    guint b_num = 0;

    /* try simple (fast) ASCII comparison first */
    for (;; ++ap, ++bp)
    {
        /* check if the characters differ or we have a non-ASCII char
         */
        ac = *((const guchar *) ap);
        bc = *((const guchar *) bp);
        if (ac != bc || ac == 0 || ac > 127)
            break;
    }

    /* fallback to Unicode comparison */
    if (G_UNLIKELY (ac > 127 || bc > 127))
    {
        for (;; ap = g_utf8_next_char (ap), bp = g_utf8_next_char (bp))
        {
            /* check if characters differ or end of string */
            ac = g_utf8_get_char (ap);
            bc = g_utf8_get_char (bp);
            if (ac != bc || ac == 0)
                break;
        }
    }
Stephan Arts's avatar
Stephan Arts committed
1112

1113
    /* If both strings are equal, we're done */
Stephan Arts's avatar
Stephan Arts committed
1114 1115 1116 1117 1118
    if (ac == bc)
    {
        return 0;
    }
    else
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
    {
        if (G_UNLIKELY (g_ascii_isdigit (ac) || g_ascii_isdigit (bc)))
        {
            /* if both strings differ in a digit, we use a smarter comparison
             * to get sorting 'file1', 'file5', 'file10' done the right way.
             */
            if (g_ascii_isdigit (ac) && g_ascii_isdigit (bc))
            {
                a_num = strtoul (ap, NULL, 10); 
                b_num = strtoul (bp, NULL, 10); 

                if (a_num < b_num)
                    result = -1;
                if (a_num > b_num)
                    result = 1;
            }
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148

            if (ap > a_base &&
                bp > b_base &&
                g_ascii_isdigit (*(ap -1)) &&
                g_ascii_isdigit (*(bp -1)) )
            {
                a_num = strtoul (ap-1, NULL, 10); 
                b_num = strtoul (bp-1, NULL, 10); 

                if (a_num < b_num)
                    result = -1;
                if (a_num > b_num)
                    result = 1;
            }
1149 1150
        }
    }
Stephan Arts's avatar
Stephan Arts committed
1151

1152 1153 1154 1155
    if (result == 0)
    {
        if (ac > bc)
            result = 1;
Stephan Arts's avatar
Stephan Arts committed
1156
        if (ac < bc)
1157 1158 1159 1160
            result = -1;
    }


Stephan Arts's avatar
Stephan Arts committed
1161 1162 1163 1164 1165 1166 1167 1168
    return result;
}

/**
 * cb_rstto_image_list_exif_date_compare_func:
 * @a:
 * @b:
 *
1169
 * TODO: Use EXIF data if available, not the last-modification-time.
Stephan Arts's avatar
Stephan Arts committed
1170 1171 1172 1173
 *
 * Return value: (see strcmp)
 */
static gint
Stephan Arts's avatar
Stephan Arts committed
1174
cb_rstto_image_list_exif_date_compare_func (RsttoFile *a, RsttoFile *b)
Stephan Arts's avatar
Stephan Arts committed
1175
{
1176 1177 1178 1179 1180 1181 1182 1183
    guint64 a_t = rstto_file_get_modified_time (a);
    guint64 b_t = rstto_file_get_modified_time (b);

    if (a_t < b_t)
    {
        return -1;
    }
    return 1;
1184
}
1185 1186 1187 1188 1189 1190 1191

gboolean
rstto_image_list_iter_get_sticky (
        RsttoImageListIter *iter)
{
    return iter->priv->sticky;
}
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211

static void
cb_rstto_wrap_images_changed (
        GObject *settings,
        GParamSpec *pspec,
        gpointer user_data)
{
    GValue val_wrap_images = { 0, };

    RsttoImageList *image_list = RSTTO_IMAGE_LIST (user_data);

    g_value_init (&val_wrap_images, G_TYPE_BOOLEAN);

    g_object_get_property (
            settings,
            "wrap-images",
            &val_wrap_images);

    image_list->priv->wrap_images = g_value_get_boolean (&val_wrap_images);
}
Stephan Arts's avatar
Stephan Arts committed
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342