File: Thumbview.cc

package info (click to toggle)
nitrogen 1.5.2-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,368 kB
  • ctags: 472
  • sloc: sh: 9,934; cpp: 2,600; ansic: 333; makefile: 102; sed: 16
file content (779 lines) | stat: -rw-r--r-- 22,332 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
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
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
404
405
406
407
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
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
736
737
738
739
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
766
767
768
769
770
771
772
773
774
775
776
777
778
779
/*

This file is from Nitrogen, an X11 background setter.  
Copyright (C) 2006  Dave Foster & Javeed Shaikh

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.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

*/

#include "main.h"
#include "Thumbview.h"
#include "md5.h"
#include <glib/gstdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "Config.h"
#include "Util.h"
#include "gcs-i18n.h"
#include "string.h"

typedef std::pair<Glib::ustring, Glib::ustring> PairStrs;

/**
 * Returns the last modified time of a file.
 * @param file The name of the file to get the modified time for.
 */
static time_t get_file_mtime(std::string file) {
	struct stat buf;
	if (stat(file.c_str(), &buf) == -1) return 0; // error
	return buf.st_mtime;
}

/**
 * Returns the value of the "tEXt::Thumb::MTime" key for fd.o style thumbs.
 * @param pixbuf The pixbuf of the fd.o thumbnail.
 */
static time_t get_fdo_thumbnail_mtime(Glib::RefPtr<Gdk::Pixbuf> pixbuf) {
	std::string mtime_str = pixbuf->get_option("tEXt::Thumb::MTime");
	std::stringstream stream(mtime_str);
	time_t mtime = 0;
	stream >> mtime;
	return mtime;
}

void DelayLoadingStore::get_value_vfunc (const iterator& iter, int column, Glib::ValueBase& value) const
{
	g_async_queue_ref(aqueue_loadthumbs);
	Gtk::ListStore::get_value_vfunc(iter, column, value);

	Gtk::TreeModel::Row row = *iter;
	if (column == 0)
	{
		Glib::Value< Glib::RefPtr<Gdk::Pixbuf> > base;
		Gtk::ListStore::get_value_vfunc(iter, column, base);

		Glib::RefPtr<Gdk::Pixbuf> thumb = base.get();

		if (thumb == thumbview->loading_image && !row[thumbview->record.LoadingThumb])
		{
			TreePair* tp = new TreePair();
			tp->file = row[thumbview->record.Filename];
			tp->iter = iter;

			row[thumbview->record.LoadingThumb] = true;

			Util::program_log("Custom model: planning on loading %s\n", tp->file.c_str());

			g_async_queue_push(aqueue_loadthumbs, (gpointer)tp);
		}
	}
	g_async_queue_unref(aqueue_loadthumbs);
}

/**
 * Constructor, sets up gtk stuff, inits data and queues
 */
Thumbview::Thumbview()
{
	set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
	set_shadow_type (Gtk::SHADOW_IN); 

    // load map of setbgs (need this for add_file)
    load_map_setbgs();

	// make our async queues
	this->aqueue_loadthumbs = g_async_queue_new();
	this->aqueue_createthumbs = g_async_queue_new();
	this->aqueue_donethumbs = g_async_queue_new();	

	// create store
	store = DelayLoadingStore::create (record);
	store->set_queue(aqueue_loadthumbs);
	store->set_thumbview(this);
	
	// setup view
    m_curmode = LIST;
    iview.set_model(store);
    iview.signal_item_activated().connect(sigc::mem_fun(*this, &Thumbview::sighandle_iview_activated));

	view.set_model (store);
	view.set_headers_visible (FALSE);
	view.set_fixed_height_mode (TRUE);
	view.set_rules_hint (TRUE);
    view.signal_row_activated().connect(sigc::mem_fun(*this, &Thumbview::sighandle_view_activated));
	
	// set cell renderer proprties
	rend.property_ellipsize () = Pango::ELLIPSIZE_END;
	rend.set_property ("ellipsize", Pango::ELLIPSIZE_END);
	rend.property_weight () = Pango::WEIGHT_BOLD;

	rend_img.set_fixed_size(105, 82);    
	
	// make treeviewcolumns
	this->col_thumb = new Gtk::TreeViewColumn("thumbnail", this->rend_img);
	this->col_desc = new Gtk::TreeViewColumn("description", this->rend);
	
	col_thumb->add_attribute (rend_img, "pixbuf", record.Thumbnail);
	col_thumb->set_sizing(Gtk::TREE_VIEW_COLUMN_FIXED);
	col_thumb->set_fixed_width(105);
	col_desc->add_attribute (rend, "markup", record.Description);
	col_desc->set_sort_column (record.Filename);
	col_desc->set_sort_indicator (true);
	col_desc->set_sort_order (Gtk::SORT_ASCENDING);
	col_desc->set_sizing(Gtk::TREE_VIEW_COLUMN_FIXED);
	
	view.append_column (*col_thumb);
	view.append_column (*col_desc);
    
    iview.set_pixbuf_column(record.Thumbnail);
    if (m_icon_captions)
        iview.set_markup_column(record.Description);
    iview.set_tooltip_column(1);
    iview.set_margin(0);
    iview.set_column_spacing(1);
    iview.set_row_spacing(1);

	// enable search
	view.set_search_column (record.Description);
	view.set_search_equal_func (sigc::mem_fun (this, &Thumbview::search_compare));

	// load loading image, which not all themes seem to provide
	try {
		this->loading_image = Gtk::IconTheme::get_default()->load_icon("image-loading", 64, Gtk::ICON_LOOKUP_FORCE_SVG);
	} catch (Gtk::IconThemeError e) { std::cerr << "ERR: Could not load \"image-loading\" from icon theme, this indicates a problem with your Gtk/Gtkmm install.\n"; }
      catch (Gdk::PixbufError e) { std::cerr << "ERR: Could not load \"image-loading\" from icon theme, this indicates a problem with your Gtk/Gtkmm install.\n"; }

	// init dispatcher 
	this->dispatch_thumb.connect(sigc::mem_fun(this, &Thumbview::handle_dispatch_thumb)); 

	col_desc->set_expand ();

	show_all();
}

/**
 * Destructor
 */
Thumbview::~Thumbview() {
	g_async_queue_unref(this->aqueue_loadthumbs);
	g_async_queue_unref(this->aqueue_createthumbs);
	g_async_queue_unref(this->aqueue_donethumbs);
}

/**
 * Adds the given file to the tree view and pushes it onto the thumbnail
 * creation queue.
 *
 * @param filename The name of the file to add.
 * @param rootdir  The root directory the file came from.
 *
 */
void Thumbview::add_file(std::string filename, std::string rootdir)
{
    Gtk::Window *window = dynamic_cast<Gtk::Window*>(get_toplevel());
	Gtk::TreeModel::iterator iter = this->store->append ();
	Gtk::TreeModel::Row row = *iter;
	Glib::RefPtr<Gdk::Pixbuf> thumb = this->loading_image; 
	row[record.Thumbnail] = thumb;
	row[record.Filename] = filename;

    int idx = filename.rfind("/");
	row[record.Description] = Glib::ustring(filename.substr(idx+1));

    row[record.RootDir] = rootdir;

    for (std::map<Glib::ustring, Glib::ustring>::iterator i = map_setbgs.begin(); i!=map_setbgs.end(); i++)
    {
        if (filename == (*i).second)
        {
            row[record.CurBGOnDisp] = (*i).first;
            row[record.Description] = Util::make_current_set_string(window, filename, (*i).first);
        }
    }

	// for modified time
	row[record.Time] = get_file_mtime(filename);

	Util::program_log("add_file(): Adding file %s (from %s)\n", filename.c_str(), rootdir.c_str());

	// push it on the thumb queue
//	TreePair *tp = new TreePair();
//	tp->file = filename;
//	tp->iter = iter;

//	queue_thumbs.push(tp);
}

void Thumbview::load_dir(std::string dir)
{
    VecStrs dirs;
    dirs.push_back(dir);
    load_dir(dirs);
}

/**
 * Opens the internal directory and starts reading files into the async queue.
 */
void Thumbview::load_dir(const VecStrs& dirs)
{
	std::queue<PairStrs> queue_dirs;
	Glib::Dir *dirhandle;	
    VecStrs dir_list;       // full list of the dirs we've seen so we don't get dups

    for (VecStrs::const_iterator i = dirs.begin(); i != dirs.end(); i++)
    {
        if (std::find(dir_list.begin(), dir_list.end(), *i) == dir_list.end())
        {
            dir_list.push_back(*i);
            queue_dirs.push(PairStrs(*i, *i));
            m_list_loaded_rootdirs.push_back(*i);
        }
    }
	
	// loop it
	while ( ! queue_dirs.empty() ) {

		// pop first
        PairStrs curpair = queue_dirs.front();

		Glib::ustring curdir = curpair.first;
        Glib::ustring rootdir = curpair.second;
		queue_dirs.pop();
		
		try {
			dirhandle = new Glib::Dir(curdir);
			Util::program_log("load_dir(): Opening dir %s\n", curdir.c_str());

		} catch (Glib::FileError e) {
			std::cerr << _("Could not open dir") << " " << curdir << ": " << e.what() << "\n";
			continue;
		}

#ifdef USE_INOTIFY

		// check if we're already monitoring this dir.
		if (watches.find(curdir) == watches.end()) {
			// this dir was successfully opened. monitor it for changes with inotify.
			// the Watch will be cleaned up automatically if the dir is deleted.
			Inotify::Watch * watch = Inotify::Watch::create(curdir);
			if (watch) {
				// no error occurred.

				// emitted when a file is deleted in this dir.
				watch->signal_deleted.connect(sigc::mem_fun(this, &Thumbview::file_deleted_callback));
				// emitted when a file is modified or created in this dir.
				watch->signal_write_closed.connect(sigc::mem_fun(this, &Thumbview::file_changed_callback));
				// two signals that are emitted when a file is renamed in this dir.
				// the best way to handle this IMO is to remove the file upon receiving
				// 'moved_from', and then to add the file upon receiving 'moved_to'.
				watch->signal_moved_from.connect(sigc::mem_fun(this, &Thumbview::file_deleted_callback));
				watch->signal_moved_to.connect(sigc::mem_fun(this, &Thumbview::file_changed_callback));
				watch->signal_created.connect(sigc::mem_fun(this, &Thumbview::file_created_callback));

				watches[curdir] = watch;
			}
		}

#endif

		for (Glib::Dir::iterator i = dirhandle->begin(); i != dirhandle->end(); i++) {
			Glib::ustring fullstr = curdir + Glib::ustring("/");
			try {
				fullstr += /*Glib::filename_to_utf8(*/*i;//);
			} catch (Glib::ConvertError& error) {
				std::cerr << _("Invalid UTF-8 encountered. Skipping file") << " " << *i << std::endl;
				continue;
			}

			if ( Glib::file_test(fullstr, Glib::FILE_TEST_IS_DIR) )
			{
				if ( Config::get_instance()->get_recurse() )
                {
                    if (std::find(dir_list.begin(), dir_list.end(), fullstr) == dir_list.end())
                    {
                        dir_list.push_back(fullstr);
                        queue_dirs.push(PairStrs(fullstr, rootdir));
                    }
                }
			}
			else {			
				if ( this->is_image(fullstr) ) {
                    add_file(fullstr, rootdir);
				}
			}
		}

		delete dirhandle;
	}
}

/**
 * Unloads a directory from the view.
 *
 * This includes all subdirectories if the add was recursive. It looks at the RootDir column
 * in the model to determine if it is a candidate to remove.
 */
void Thumbview::unload_dir(std::string dir)
{
    Gtk::TreeIter iter;
    for (iter = store->children().begin(); iter != store->children().end(); )
    {
        if ((*iter)[record.RootDir] == dir)
            iter = store->erase(iter);
        else
            iter++;
    }
}

/**
 * Sets the selection to the passed in tree iterator.
 * This is a timeout function.
 *
 * Respects the currently selected view mode.
 * @param   iter        The item in the tree model to select.
 */
bool Thumbview::select(const Gtk::TreeModel::iterator *iter)
{
    Gtk::TreeModel::Path path(*iter);

    Glib::TimeVal curtime;
    curtime.assign_current_time();

    // make sure we've given it enough time
    curtime.subtract(m_last_loaded_time);
    if (curtime.as_double() < 0.1)
        return true;


    if (m_curmode == ICON)
    {
        iview.select_path(path);
        iview.scroll_to_path(path, true, 0.5, 0.5);
    }
    else
    {
        view.get_selection()->select(*iter);
        view.scroll_to_row(path, 0.5);
    }

    delete iter;
    return false;
}

/**
 * Tests the file to see if it is an image
 * TODO: come up with less sux way of doing it than extension
 *
 * @param	file	The filename to test
 * @return			If its an image or not
 */
bool Thumbview::is_image(std::string file) {
	if (file.find (".png")  != std::string::npos ||
		file.find (".PNG")  != std::string::npos || 
		file.find (".jpg")  != std::string::npos || 
		file.find (".JPG")  != std::string::npos || 
		file.find (".jpeg") != std::string::npos || 
		file.find (".JPEG") != std::string::npos ||
		file.find (".gif")  != std::string::npos ||
		file.find (".GIF")  != std::string::npos ) 
		return true;

	return false;
}

/**
 * Determines the full path to a cache file.  Does all creation of dirs.
 * TODO: should throw exception if we cannot create the dirs!
 *
 * @param	file	The file to convert, calls cache_name on it
 * @return			The full path to 
 */
Glib::ustring Thumbview::cache_file(Glib::ustring file) {
	
	Glib::ustring urifile = Glib::filename_to_uri(file);

	// compute md5 of file's uri
	md5_state_t state;
	md5_byte_t digest[16];
	md5_init(&state);
	md5_append(&state, (const md5_byte_t *)urifile.c_str(), strlen(urifile.c_str()));
	md5_finish(&state, digest);

	char *buf = new char[3];
	char *full = new char[33];
	full[0] = '\0';
	
	for (int di = 0; di < 16; ++di) {
		sprintf(buf, "%02x", digest[di]);
		strcat(full, buf);
	}

	Glib::ustring md5file = Glib::ustring(full) + Glib::ustring(".png");
	delete [] buf;
	delete [] full;

	// build dir paths
	Glib::ustring halfref = Glib::build_filename(Glib::get_home_dir(),".thumbnails/");
	halfref = Glib::build_filename(halfref, "normal/");

	if ( !Glib::file_test(halfref, Glib::FILE_TEST_EXISTS) )
		if ( g_mkdir_with_parents(halfref.c_str(), 0700) == -1)
			// TODO: exception
			return "FAIL";

	// add and return
	return Glib::build_filename(halfref, md5file);
}

/**
 * Creates cache images that show up in its queue.  
 */
void Thumbview::load_cache_images() 
{
	g_async_queue_ref(this->aqueue_loadthumbs); 
	g_async_queue_ref(this->aqueue_donethumbs); 

	while(1)
	{
		// remove first item (blocks until an item occurs)
		TreePair *p = (TreePair*)g_async_queue_pop(this->aqueue_loadthumbs);

		Glib::ustring file = p->file;
		Glib::ustring cachefile = this->cache_file(file);

		// branch to see if we need to load or create cache file
		if ( !Glib::file_test(cachefile, Glib::FILE_TEST_EXISTS) ) {
			g_async_queue_push(this->aqueue_createthumbs,(gpointer)p);
		} else {
			// load thumb
			Glib::RefPtr<Gdk::Pixbuf> pb = Gdk::Pixbuf::create_from_file(this->cache_file(file));

			// resize if we need to
			if (pb->get_width() > 100 || pb->get_height() > 80)
			{
				int pbwidth = pb->get_width();
				int pbheight = pb->get_height();
				float ratio = (float)pbwidth / (float)pbheight;
				
				int newwidth, newheight;

				if (abs(100 - pbwidth) > abs(80 - pbheight))
				{
					// cap to vertical
					newheight = 80;
					newwidth = newheight * ratio;
				}
				else
				{
					// cap to horiz
					newwidth = 100;
					newheight = newwidth / ratio;
				}

				pb = pb->scale_simple(newwidth, newheight, Gdk::INTERP_NEAREST);
			}

			if (get_fdo_thumbnail_mtime(pb) < get_file_mtime(file)) {
				// the thumbnail is old. we need to make a new one.
				pb.clear();
				g_async_queue_push(this->aqueue_createthumbs,(gpointer)p);
			} else {
				// display it
				TreePair *sendp = new TreePair();
				sendp->file = file;
				sendp->iter = p->iter;
				sendp->thumb = pb;

				g_async_queue_push(this->aqueue_donethumbs, (gpointer)sendp);

				this->dispatch_thumb.emit();

				delete p;
			}
		}
	}
	
	g_async_queue_unref(this->aqueue_loadthumbs); 
	g_async_queue_unref(this->aqueue_donethumbs); 
	throw Glib::Thread::Exit();
}

/**
 * Thread function to create thumbnail cache images for those that do not exist.
 */
void Thumbview::create_cache_images()
{
	Glib::RefPtr<Gdk::Pixbuf> thumb;

	g_async_queue_ref(this->aqueue_createthumbs); 
	g_async_queue_ref(this->aqueue_donethumbs); 

	// always work! 
	while (1) {

		// remove first item (blocks when no items occur)
		TreePair *p = (TreePair*)g_async_queue_pop(this->aqueue_createthumbs);

		// get filenames
		Glib::ustring file = p->file;
		Glib::ustring cachefile = this->cache_file(file);

		Util::program_log("create_cache_images(): + Caching file %s\n", file.c_str());

		// open image, scale to smallish 4:3 with aspect preserved
		try {
			thumb = Gdk::Pixbuf::create_from_file(file, 100, 75, true);
		} catch (...) {
			// forget it, move on
			delete p;
			continue;
		}

		// create required fd.o png tags
		std::list<Glib::ustring> opts, vals;
		opts.push_back(Glib::ustring("tEXt::Thumb::URI"));
		vals.push_back(Glib::filename_to_uri(file));

		time_t mtime = get_file_mtime(file);

		char *bufout = new char[20];
		sprintf(bufout, "%d", mtime);

		opts.push_back(Glib::ustring("tEXt::Thumb::MTime"));
		vals.push_back(Glib::ustring(bufout));

		delete [] bufout;
				
		thumb->save(cachefile, "png", opts, vals);

        Util::program_log("create_cache_images(): - Finished caching file %s\n", file.c_str());

		// send it to the display
		TreePair *sendp = new TreePair();
		sendp->file = file;
		sendp->iter = p->iter;
		sendp->thumb = thumb;
		g_async_queue_push(this->aqueue_donethumbs, (gpointer)sendp);

		// emit dispatcher
		this->dispatch_thumb.emit();

		delete p;
	}

	g_async_queue_unref(this->aqueue_createthumbs); 
	g_async_queue_unref(this->aqueue_donethumbs); 
	throw Glib::Thread::Exit();
}

void Thumbview::handle_dispatch_thumb() { 
	g_async_queue_ref(this->aqueue_donethumbs);

	TreePair *donep = (TreePair*)g_async_queue_pop(this->aqueue_donethumbs);
	this->update_thumbnail(donep->file, donep->iter, donep->thumb);
	delete donep;

    update_last_loaded_time();

	g_async_queue_unref(this->aqueue_donethumbs);
}

/**
 * Updates the treeview to show the passed in thumbnail.
 *
 */
void Thumbview::update_thumbnail(Glib::ustring file, Gtk::TreeModel::iterator iter, Glib::RefPtr<Gdk::Pixbuf> pb)
{
	Gtk::TreeModel::Row row = *iter;
	row[record.Thumbnail] = pb;
	// desc
	//row[record.Description] = Glib::ustring(file, file.rfind("/")+1);

	// emit a changed signal
	store->row_changed(store->get_path(iter), iter);	
}

/**
 * Used by GTK to see whether or not an iterator matches a search string.
 */
bool Thumbview::search_compare (const Glib::RefPtr<Gtk::TreeModel>& model, int column, const Glib::ustring& key, const Gtk::TreeModel::iterator& iter) {
	Glib::ustring target = (*iter)[record.Description];
	if (target.find (key) != Glib::ustring::npos) {
		return false;
	}
	return true;
}

/**
 * Sets the sort mode. This tells the view how it should sort backgrounds.
 */
void Thumbview::set_sort_mode (Thumbview::SortMode mode) {
	switch (mode) {
		case SORT_ALPHA:
			store->set_sort_column (record.Description, Gtk::SORT_ASCENDING);
			break;
		case SORT_RALPHA:
			store->set_sort_column (record.Description, Gtk::SORT_DESCENDING);
			break;
		case SORT_TIME:
			store->set_sort_column (record.Time, Gtk::SORT_ASCENDING);
			break;
		case SORT_RTIME:
			store->set_sort_column (record.Time, Gtk::SORT_DESCENDING);
			break;
	}
}

/**
 * Loads the map of displays and their set bgs. Used to indicate which
 * files are currently set as a background.
 */
void Thumbview::load_map_setbgs()
{
    map_setbgs.clear();
    
    std::vector<Glib::ustring> vecgroups;
    bool ret = Config::get_instance()->get_bg_groups(vecgroups);
    if (!ret)
        return;

    for (std::vector<Glib::ustring>::iterator i = vecgroups.begin(); i!=vecgroups.end(); i++)
    {
        Glib::ustring file;
        SetBG::SetMode mode;
        Gdk::Color bgcolor;

        ret = Config::get_instance()->get_bg(*i, file, mode, bgcolor);
        if (!ret)
        {
            std::cerr << "(load_map_setbgs) Could not get background stored info for " << *i << "\n";
            return;
        }

        map_setbgs[*i] = file;
    }
}

/**
 * Gets the currently selected image, regardless of which view it came from.
 */
Gtk::TreeModel::iterator Thumbview::get_selected()
{
    if (m_curmode == ICON)
    {    
        std::list<Gtk::TreePath> selected = iview.get_selected_items();
        return store->get_iter(*(selected.begin()));
    }
    else
    {
        return view.get_selection()->get_selected();
    }
}

#ifdef USE_INOTIFY

/**
 * Called when a file in a directory being monitored by inotify is deleted.
 * Removes the file from the tree view.
 *
 * @param filename The name of the file to remove.
 */
void Thumbview::file_deleted_callback(std::string filename) {
	if (watches.find(filename) != watches.end()) {
		watches.erase(filename);
		return;
	}
	Gtk::TreeIter iter;
	Gtk::TreeModel::Children children = store->children();
	for (iter = children.begin(); iter != children.end(); iter++) {
		Glib::ustring this_filename = (*iter)[record.Filename];
		if (this_filename == filename) {
			// remove this iter.
			store->erase(iter);
			break;
		}
	}
}

/**
 * Called when a file in a directory being monitored by inotify is modified
 * or a new file is created. Adds the file to the tree view.
 *
 * @param filename The name of the file modified or created.
 */
void Thumbview::file_changed_callback(std::string filename) {
	// first remove the old instance of this file.
	file_deleted_callback(filename);
	if ( Glib::file_test(filename, Glib::FILE_TEST_IS_DIR) ) {
		// this is a directory; use load_dir() to set us up the bomb.
		load_dir(filename);
	} else if (is_image(filename)) {
		// this is a file.
        std::string rootdir = "";
		add_file(filename, rootdir);
	}
	// restart the idle function
	//Glib::signal_idle().connect(sigc::mem_fun(this, &Thumbview::load_cache_images));
}

/**
 * Called when a new file or directory is created in a directory being
 * monitored. Discards the event for non-directories, because
 * file_changed_callback will be called for those.
 *
 * @param filename The name of the file modified or created.
 */
void Thumbview::file_created_callback(std::string filename) {
	if ( Glib::file_test(filename, Glib::FILE_TEST_IS_DIR) ) {
		file_changed_callback(filename);
	}
}

#endif

void Thumbview::set_current_display_mode(DisplayMode newmode)
{
    remove();
    if (newmode == LIST)
        add(view);
    else if (newmode == ICON)
        add(iview);

    m_curmode = newmode;
    show_all();
}

void Thumbview::set_icon_captions(gboolean caps)
{
    m_icon_captions = caps;
    if (m_icon_captions)
        iview.set_markup_column(record.Description);
    else
        iview.set_markup_column(-1);  // -1 unsets
}

void Thumbview::sighandle_iview_activated(const Gtk::TreePath& path)
{
    signal_selected(path);
}

void Thumbview::sighandle_view_activated(const Gtk::TreePath& path, Gtk::TreeViewColumn *column)
{
    signal_selected(path);
}