image_list.c 21.9 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 18
 *
 *  Sorting-algorithm taken from the thunar filemanager.
19 20 21 22 23 24 25
 */

#include <config.h>

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

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

#include <libexif/exif-data.h>

Stephan Arts's avatar
Stephan Arts committed
31
#include "util.h"
Stephan Arts's avatar
Stephan Arts committed
32
#include "file.h"
33
#include "image_list.h"
34
#include "settings.h"
35 36 37 38 39 40 41 42

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

51 52 53 54 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);

static RsttoImageListIter * rstto_image_list_iter_new ();

static gint
Stephan Arts's avatar
Stephan Arts committed
61
cb_rstto_image_list_image_name_compare_func (RsttoFile *a, RsttoFile *b);
Stephan Arts's avatar
Stephan Arts committed
62
static gint
Stephan Arts's avatar
Stephan Arts committed
63
cb_rstto_image_list_exif_date_compare_func (RsttoFile *a, RsttoFile *b);
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

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

enum
{
    RSTTO_IMAGE_LIST_SIGNAL_NEW_IMAGE = 0,
    RSTTO_IMAGE_LIST_SIGNAL_REMOVE_IMAGE,
    RSTTO_IMAGE_LIST_SIGNAL_REMOVE_ALL,
    RSTTO_IMAGE_LIST_SIGNAL_COUNT
};

enum
{
    RSTTO_IMAGE_LIST_ITER_SIGNAL_CHANGED = 0,
79
    RSTTO_IMAGE_LIST_ITER_SIGNAL_PREPARE_CHANGE,
80 81 82 83 84 85
    RSTTO_IMAGE_LIST_ITER_SIGNAL_COUNT
};

struct _RsttoImageListIterPriv
{
    RsttoImageList *image_list;
Stephan Arts's avatar
Stephan Arts committed
86
    RsttoFile *file;
87 88 89 90
};

struct _RsttoImageListPriv
{
Stephan Arts's avatar
Stephan Arts committed
91
    GFileMonitor *monitor;
Stephan Arts's avatar
Stephan Arts committed
92

Stephan Arts's avatar
Stephan Arts committed
93 94 95 96 97
    GList        *images;
    gint          n_images;

    GSList       *iterators;
    GCompareFunc  cb_rstto_image_list_compare_func;
98 99 100 101 102 103
};

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
104
rstto_image_list_get_type (void)
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
{
    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);
    }
    return rstto_image_list_type;
}

static void
rstto_image_list_init(RsttoImageList *image_list)
{
132

133
    image_list->priv = g_new0 (RsttoImageListPriv, 1);
Stephan Arts's avatar
Stephan Arts committed
134
    image_list->priv->cb_rstto_image_list_compare_func = (GCompareFunc)cb_rstto_image_list_image_name_compare_func;
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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
}

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_NEW_IMAGE] = g_signal_new("new-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_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)
{
    /*RsttoImageList *image_list = RSTTO_IMAGE_LIST(object);*/
}

RsttoImageList *
Stephan Arts's avatar
Stephan Arts committed
189
rstto_image_list_new (void)
190 191 192 193 194 195 196 197 198
{
    RsttoImageList *image_list;

    image_list = g_object_new(RSTTO_TYPE_IMAGE_LIST, NULL);

    return image_list;
}

gboolean
Stephan Arts's avatar
Stephan Arts committed
199
rstto_image_list_add_file (RsttoImageList *image_list, RsttoFile *file, GError **error)
200
{
Stephan Arts's avatar
Stephan Arts committed
201
    GList *image_iter = g_list_find (image_list->priv->images, file);
202

Stephan Arts's avatar
Stephan Arts committed
203 204
    if (!image_iter)
    {
205
        if (file)
Stephan Arts's avatar
Stephan Arts committed
206
        {
207 208
            image_list->priv->images = g_list_insert_sorted (image_list->priv->images, file, rstto_image_list_get_compare_func (image_list));

Stephan Arts's avatar
Stephan Arts committed
209 210
            image_list->priv->n_images++;

211
            g_signal_emit (G_OBJECT (image_list), rstto_image_list_signals[RSTTO_IMAGE_LIST_SIGNAL_NEW_IMAGE], 0, file, NULL);
Stephan Arts's avatar
Stephan Arts committed
212
            if (image_list->priv->n_images == 1)
Stephan Arts's avatar
Stephan Arts committed
213
            {
Stephan Arts's avatar
Stephan Arts committed
214 215 216 217 218 219 220
                /** TODO: update all iterators */
                GSList *iter = 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
221
            }
Stephan Arts's avatar
Stephan Arts committed
222
            return TRUE;
Stephan Arts's avatar
Stephan Arts committed
223
        }
Stephan Arts's avatar
Stephan Arts committed
224
        return FALSE;
225
    }
Stephan Arts's avatar
Stephan Arts committed
226 227 228
    g_signal_emit (G_OBJECT (image_list), rstto_image_list_signals[RSTTO_IMAGE_LIST_SIGNAL_NEW_IMAGE], 0, image_iter->data, NULL);

    return TRUE;
229 230 231 232 233 234 235 236
}

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
237 238 239 240 241 242 243 244
/**
 * rstto_image_list_get_iter:
 * @image_list:
 *
 * TODO: track iterators
 *
 * return iter;
 */
245 246 247
RsttoImageListIter *
rstto_image_list_get_iter (RsttoImageList *image_list)
{
Stephan Arts's avatar
Stephan Arts committed
248
    RsttoFile *file = NULL;
Stephan Arts's avatar
Stephan Arts committed
249
    RsttoImageListIter *iter = NULL;
250
    if (image_list->priv->images)
251
        file = image_list->priv->images->data;
252

253
    iter = rstto_image_list_iter_new (image_list, file);
254

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

257 258 259 260 261
    return iter;
}


void
Stephan Arts's avatar
Stephan Arts committed
262
rstto_image_list_remove_file (RsttoImageList *image_list, RsttoFile *file)
263
{
Stephan Arts's avatar
Stephan Arts committed
264
    GSList *iter = NULL;
Stephan Arts's avatar
Stephan Arts committed
265
    RsttoFile *afile = NULL;
Stephan Arts's avatar
Stephan Arts committed
266

267
    if (g_list_find(image_list->priv->images, file))
268
    {
Stephan Arts's avatar
Stephan Arts committed
269

Stephan Arts's avatar
Stephan Arts committed
270
        iter = image_list->priv->iterators;
Stephan Arts's avatar
Stephan Arts committed
271 272
        while (iter)
        {
Stephan Arts's avatar
Stephan Arts committed
273
            if (rstto_file_equal(rstto_image_list_iter_get_file (iter->data), file))
Stephan Arts's avatar
Stephan Arts committed
274
            {
275
                if (rstto_image_list_iter_get_position (iter->data) == rstto_image_list_get_n_images (image_list)-1)
276
                {
277
                    rstto_image_list_iter_previous (iter->data);
278 279 280
                }
                else
                {
281
                    rstto_image_list_iter_next (iter->data);
282
                }
283 284 285 286
                /* 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
287
                if (rstto_file_equal(rstto_image_list_iter_get_file (iter->data), file))
288
                {
289
                    ((RsttoImageListIter *)(iter->data))->priv->file = NULL;
290
                    g_signal_emit (G_OBJECT (iter->data), rstto_image_list_iter_signals[RSTTO_IMAGE_LIST_ITER_SIGNAL_CHANGED], 0, NULL);
291
                }
Stephan Arts's avatar
Stephan Arts committed
292 293 294
            }
            iter = g_slist_next (iter);
        }
295

296
        image_list->priv->images = g_list_remove (image_list->priv->images, file);
297 298 299
        iter = image_list->priv->iterators;
        while (iter)
        {
300 301
            afile = rstto_image_list_iter_get_file(iter->data);
            if (NULL != afile)
302
            {
Stephan Arts's avatar
Stephan Arts committed
303
                if (rstto_file_equal(afile, file))
304
                {
305
                    rstto_image_list_iter_next (iter->data);
306
                }
307 308 309 310
            }
            iter = g_slist_next (iter);
        }

311 312
        g_signal_emit (G_OBJECT (image_list), rstto_image_list_signals[RSTTO_IMAGE_LIST_SIGNAL_REMOVE_IMAGE], 0, file, NULL);
        g_object_unref(file);
313 314 315 316 317 318
    }
}

void
rstto_image_list_remove_all (RsttoImageList *image_list)
{
Stephan Arts's avatar
Stephan Arts committed
319
    GSList *iter = NULL;
320 321 322
    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
323

Stephan Arts's avatar
Stephan Arts committed
324
    iter = image_list->priv->iterators;
Stephan Arts's avatar
Stephan Arts committed
325 326
    while (iter)
    {
Stephan Arts's avatar
Stephan Arts committed
327
        rstto_image_list_iter_set_position (iter->data, -1);
Stephan Arts's avatar
Stephan Arts committed
328 329 330
        iter = g_slist_next (iter);
    }
    g_signal_emit (G_OBJECT (image_list), rstto_image_list_signals[RSTTO_IMAGE_LIST_SIGNAL_REMOVE_ALL], 0, NULL);
331 332
}

Stephan Arts's avatar
Stephan Arts committed
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 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 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
void
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;
    }

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

    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:
            g_debug("file deleted");
            rstto_image_list_remove_file ( image_list, r_file );
            r_file = NULL;
            break;
        case G_FILE_MONITOR_EVENT_CREATED:
            g_debug("file created");
            rstto_image_list_add_file (image_list, r_file, NULL);
            r_file = NULL;
            break;
        case G_FILE_MONITOR_EVENT_MOVED:
            g_debug("file moved");
            rstto_image_list_remove_file ( image_list, r_file );

            r_file = rstto_file_new (other_file);
            rstto_image_list_add_file (image_list, r_file, NULL);
            r_file = NULL;
            break;
        default:
            break;
    }

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

404 405 406


GType
Stephan Arts's avatar
Stephan Arts committed
407
rstto_image_list_iter_get_type (void)
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
{
    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;

447 448 449 450 451 452 453 454 455 456 457
    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);

458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
    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);
475
    if (iter->priv->file)
476
    {
477
        iter->priv->file = NULL;
478
    }
Stephan Arts's avatar
Stephan Arts committed
479 480 481 482 483 484

    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;
    }
485 486 487
}

static RsttoImageListIter *
Stephan Arts's avatar
Stephan Arts committed
488
rstto_image_list_iter_new (RsttoImageList *nav, RsttoFile *file)
489 490 491 492
{
    RsttoImageListIter *iter;

    iter = g_object_new(RSTTO_TYPE_IMAGE_LIST_ITER, NULL);
493
    iter->priv->file = file;
494 495 496 497 498 499
    iter->priv->image_list = nav;

    return iter;
}

gboolean
Stephan Arts's avatar
Stephan Arts committed
500
rstto_image_list_iter_find_file (RsttoImageListIter *iter, RsttoFile *file)
501
{
502
    gint pos = g_list_index (iter->priv->image_list->priv->images, file);
503 504
    if (pos > -1)
    {
505 506
        g_signal_emit (G_OBJECT (iter), rstto_image_list_iter_signals[RSTTO_IMAGE_LIST_ITER_SIGNAL_PREPARE_CHANGE], 0, NULL);

507
        if (iter->priv->file)
508
        {
509
            iter->priv->file = NULL;
510
        }
511
        iter->priv->file = file;
512 513 514 515 516 517 518 519 520 521 522

        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)
{
523
    if ( NULL == iter->priv->file )
524
    {
525
        return -1;
526
    }
527
    return g_list_index (iter->priv->image_list->priv->images, iter->priv->file);
528 529
}

Stephan Arts's avatar
Stephan Arts committed
530
RsttoFile *
531
rstto_image_list_iter_get_file (RsttoImageListIter *iter)
532
{
533
    return iter->priv->file;
534 535 536
}


Stephan Arts's avatar
Stephan Arts committed
537
void
538 539
rstto_image_list_iter_set_position (RsttoImageListIter *iter, gint pos)
{
540 541
    g_signal_emit (G_OBJECT (iter), rstto_image_list_iter_signals[RSTTO_IMAGE_LIST_ITER_SIGNAL_PREPARE_CHANGE], 0, NULL);

542
    if (iter->priv->file)
543
    {
544
        iter->priv->file = NULL;
545 546
    }

Stephan Arts's avatar
Stephan Arts committed
547
    if (pos >= 0)
Stephan Arts's avatar
Stephan Arts committed
548
    {
549
        iter->priv->file = g_list_nth_data (iter->priv->image_list->priv->images, pos); 
Stephan Arts's avatar
Stephan Arts committed
550
    }
551

552 553 554
    g_signal_emit (G_OBJECT (iter), rstto_image_list_iter_signals[RSTTO_IMAGE_LIST_ITER_SIGNAL_CHANGED], 0, NULL);
}

555
gboolean
556 557
rstto_image_list_iter_next (RsttoImageListIter *iter)
{
558
    GList *position = NULL;
Stephan Arts's avatar
Stephan Arts committed
559
    RsttoSettings *settings = NULL;
560
    RsttoFile *file = iter->priv->file;
561
    gboolean ret_val = FALSE;
562 563 564

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

565
    if (iter->priv->file)
566
    {
567 568
        position = g_list_find (iter->priv->image_list->priv->images, iter->priv->file);
        iter->priv->file = NULL;
569 570
    }

571 572
    position = g_list_next (position);
    if (position)
573
    {
574
        iter->priv->file = position->data; 
575 576 577 578

        /* We could move forward, set ret_val to TRUE */
        ret_val = TRUE;
    }
579 580
    else
    {
Stephan Arts's avatar
Stephan Arts committed
581 582 583
        settings = rstto_settings_new();

        if (rstto_settings_get_boolean_property (settings, "wrap-images"))
584
        {
585
            position = g_list_first (iter->priv->image_list->priv->images);
586 587 588 589 590 591

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

595
        if (position)
596
            iter->priv->file = position->data; 
597
        else
598
            iter->priv->file = NULL;
Stephan Arts's avatar
Stephan Arts committed
599 600

        g_object_unref (settings);
601 602
    }

603 604 605 606
    if (file != iter->priv->file)
    {
        g_signal_emit (G_OBJECT (iter), rstto_image_list_iter_signals[RSTTO_IMAGE_LIST_ITER_SIGNAL_CHANGED], 0, NULL);
    }
607 608

    return ret_val;
609 610
}

611
gboolean
612 613
rstto_image_list_iter_previous (RsttoImageListIter *iter)
{
614
    GList *position = NULL;
Stephan Arts's avatar
Stephan Arts committed
615
    RsttoSettings *settings = NULL;
616
    RsttoFile *file = iter->priv->file;
617
    gboolean ret_val = FALSE;
618 619 620

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

621
    if (iter->priv->file)
622
    {
623 624
        position = g_list_find (iter->priv->image_list->priv->images, iter->priv->file);
        iter->priv->file = NULL;
625 626
    }

627 628
    position = g_list_previous (position);
    if (position)
629
    {
630
        iter->priv->file = position->data; 
631
    }
632 633
    else
    {
Stephan Arts's avatar
Stephan Arts committed
634 635 636
        settings = rstto_settings_new();

        if (rstto_settings_get_boolean_property (settings, "wrap-images"))
637 638 639 640
            position = g_list_last (iter->priv->image_list->priv->images);
        else
            position = g_list_first (iter->priv->image_list->priv->images);

641
        if (position)
642
            iter->priv->file = position->data; 
643
        else
644
            iter->priv->file = NULL;
Stephan Arts's avatar
Stephan Arts committed
645 646

        g_object_unref (settings);
647
    }
648

649 650 651 652
    if (file != iter->priv->file)
    {
        g_signal_emit (G_OBJECT (iter), rstto_image_list_iter_signals[RSTTO_IMAGE_LIST_ITER_SIGNAL_CHANGED], 0, NULL);
    }
653 654

    return ret_val;
655 656 657 658 659
}

RsttoImageListIter *
rstto_image_list_iter_clone (RsttoImageListIter *iter)
{
660
    RsttoImageListIter *new_iter = rstto_image_list_iter_new (iter->priv->image_list, iter->priv->file);
Stephan Arts's avatar
Stephan Arts committed
661
    rstto_image_list_iter_set_position (new_iter, rstto_image_list_iter_get_position(iter));
662 663 664

    return new_iter;
}
665 666 667 668

GCompareFunc
rstto_image_list_get_compare_func (RsttoImageList *image_list)
{
Stephan Arts's avatar
Stephan Arts committed
669 670 671
    return (GCompareFunc)image_list->priv->cb_rstto_image_list_compare_func;
}

672 673 674
void
rstto_image_list_set_compare_func (RsttoImageList *image_list, GCompareFunc func)
{
675
    GSList *iter = NULL;
676
    image_list->priv->cb_rstto_image_list_compare_func = func;
677
    image_list->priv->images = g_list_sort (image_list->priv->images,  func);
678

679 680 681 682
    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);
    }
683 684
}

Stephan Arts's avatar
Stephan Arts committed
685 686 687 688
/***********************/
/*  Compare Functions  */
/***********************/

689 690 691
void
rstto_image_list_set_sort_by_name (RsttoImageList *image_list)
{
692
    rstto_image_list_set_compare_func (image_list, (GCompareFunc)cb_rstto_image_list_image_name_compare_func);
693 694 695 696 697
}

void
rstto_image_list_set_sort_by_date (RsttoImageList *image_list)
{
698
    rstto_image_list_set_compare_func (image_list, (GCompareFunc)cb_rstto_image_list_exif_date_compare_func);
699 700
}

Stephan Arts's avatar
Stephan Arts committed
701 702 703 704 705 706 707 708 709
/**
 * cb_rstto_image_list_image_name_compare_func:
 * @a:
 * @b:
 *
 *
 * Return value: (see strcmp)
 */
static gint
Stephan Arts's avatar
Stephan Arts committed
710
cb_rstto_image_list_image_name_compare_func (RsttoFile *a, RsttoFile *b)
Stephan Arts's avatar
Stephan Arts committed
711
{
Stephan Arts's avatar
Stephan Arts committed
712 713
    const gchar *a_base = rstto_file_get_display_name (a);
    const gchar *b_base = rstto_file_get_display_name (b);
714 715 716 717 718
    guint  ac;
    guint  bc;
    const gchar *ap = a_base;
    const gchar *bp = b_base;

Stephan Arts's avatar
Stephan Arts committed
719
    gint result = 0;
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
    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
746

747
    /* If both strings are equal, we're done */
Stephan Arts's avatar
Stephan Arts committed
748 749 750 751 752
    if (ac == bc)
    {
        return 0;
    }
    else
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
    {
        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;
            }
769 770 771 772 773 774 775 776 777 778 779 780 781 782

            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;
            }
783 784
        }
    }
Stephan Arts's avatar
Stephan Arts committed
785

786 787 788 789
    if (result == 0)
    {
        if (ac > bc)
            result = 1;
Stephan Arts's avatar
Stephan Arts committed
790
        if (ac < bc)
791 792 793 794
            result = -1;
    }


Stephan Arts's avatar
Stephan Arts committed
795 796 797 798 799 800 801 802
    return result;
}

/**
 * cb_rstto_image_list_exif_date_compare_func:
 * @a:
 * @b:
 *
803
 * TODO: Use EXIF data if available, not the last-modification-time.
Stephan Arts's avatar
Stephan Arts committed
804 805 806 807
 *
 * Return value: (see strcmp)
 */
static gint
Stephan Arts's avatar
Stephan Arts committed
808
cb_rstto_image_list_exif_date_compare_func (RsttoFile *a, RsttoFile *b)
Stephan Arts's avatar
Stephan Arts committed
809
{
810 811 812 813 814 815 816 817
    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;
818
}