⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 collect.c

📁 Gqview,Linux下基于GTK+库写成的轻量级而能丰富的图像浏览程序。
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * GQview * (C) 2004 John Ellis * * Author: John Ellis * * This software is released under the GNU General Public License (GNU GPL). * Please read the included file COPYING for more information. * This software comes with no warranty of any kind, use at your own risk! */#include "gqview.h"#include "collect.h"#include "collect-dlg.h"#include "collect-io.h"#include "collect-table.h"#include "editors.h"#include "filelist.h"#include "img-view.h"#include "info.h"#include "layout.h"#include "layout_image.h"#include "utilops.h"#include "ui_fileops.h"#include "ui_tree_edit.h"#include <gdk/gdkkeysyms.h> /* for keyboard values */#include "icons/collect.xpm"#define COLLECT_DEF_WIDTH 440#define COLLECT_DEF_HEIGHT 450static GList *collection_list = NULL;static GList *collection_window_list = NULL;static void collection_window_get_geometry(CollectWindow *cw);static void collection_window_refresh(CollectWindow *cw);static void collection_window_update_title(CollectWindow *cw);static void collection_window_add(CollectWindow *cw, CollectInfo *ci);static void collection_window_insert(CollectWindow *cw, CollectInfo *ci);static void collection_window_remove(CollectWindow *cw, CollectInfo *ci);static void collection_window_update(CollectWindow *cw, CollectInfo *ci);static void collection_window_close(CollectWindow *cw);/* *------------------------------------------------------------------- * data, list handling *------------------------------------------------------------------- */CollectInfo *collection_info_new(const gchar *path, struct stat *st, GdkPixbuf *pixbuf){	CollectInfo *ci;	if (!path) return NULL;	ci = g_new0(CollectInfo, 1);	ci->path = g_strdup(path);	ci->size = st->st_size;	ci->date = st->st_mtime;	ci->pixbuf = pixbuf;	if (ci->pixbuf) g_object_ref(ci->pixbuf);	return ci;}void collection_info_free_thumb(CollectInfo *ci){	if (ci->pixbuf) g_object_unref(ci->pixbuf);	ci->pixbuf = NULL;}void collection_info_free(CollectInfo *ci){	if (!ci) return;	g_free(ci->path);	collection_info_free_thumb(ci);	g_free(ci);}void collection_info_set_thumb(CollectInfo *ci, GdkPixbuf *pixbuf){	if (pixbuf) g_object_ref(pixbuf);	collection_info_free_thumb(ci);	ci->pixbuf = pixbuf;}gint collection_info_load_thumb(CollectInfo *ci){	if (!ci) return FALSE;	collection_info_free_thumb(ci);	printf("collection_info_load_thumb not implemented!\n(because an instant thumb loader not implemented)");	return FALSE;#if 0		if (create_thumbnail(ci->path, &ci->pixmap, &ci->mask) < 0) return FALSE;	if (ci->pixmap) gdk_pixmap_ref(ci->pixmap);	if (ci->mask) gdk_bitmap_ref(ci->mask);	return TRUE;#endif}void collection_list_free(GList *list){	GList *work;	work = list;	while(work)		{		collection_info_free((CollectInfo *)work->data);		work = work->next;		}	g_list_free(list);}/* an ugly static var, well what ya gonna do ? */static SortType collection_list_sort_method = SORT_NAME;static gint collection_list_sort_cb(gconstpointer a, gconstpointer b){	const CollectInfo *cia = a;	const CollectInfo *cib = b;	switch(collection_list_sort_method)		{		case SORT_NONE:			return 0;			break;		case SORT_SIZE:			if (cia->size < cib->size) return -1;			if (cia->size > cib->size) return 1;			return 0;			break;		case SORT_TIME:			if (cia->date < cib->date) return -1;			if (cia->date > cib->date) return 1;			return 0;			break;		case SORT_PATH:			return CASE_SORT(cia->path, cib->path);			break;#ifdef HAVE_STRVERSCMP		case SORT_NUMBER:			return strverscmp(filename_from_path(cia->path), filename_from_path(cib->path));			break;#endif		case SORT_NAME:		default:			return CASE_SORT(filename_from_path(cia->path), filename_from_path(cib->path));			break;		}	return 0;}GList *collection_list_sort(GList *list, SortType method){	if (method == SORT_NONE) return list;	collection_list_sort_method = method;	return g_list_sort(list, collection_list_sort_cb);}GList *collection_list_add(GList *list, CollectInfo *ci, SortType method){	if (method != SORT_NONE)		{		collection_list_sort_method = method;		list = g_list_insert_sorted(list, ci, collection_list_sort_cb);		}	else		{		list = g_list_append(list, ci);		}	return list;}GList *collection_list_insert(GList *list, CollectInfo *ci, CollectInfo *insert_ci, SortType method){	if (method != SORT_NONE)		{		collection_list_sort_method = method;		list = g_list_insert_sorted(list, ci, collection_list_sort_cb);		}	else		{		GList *point;		point = g_list_find(list, insert_ci);		list = uig_list_insert_link(list, point, ci);		}	return list;}GList *collection_list_remove(GList *list, CollectInfo *ci){	list = g_list_remove(list, ci);	collection_info_free(ci);	return list;}CollectInfo *collection_list_find(GList *list, const gchar *path){	GList *work = list;	while(work)		{		CollectInfo *ci = work->data;		if (strcmp(ci->path, path) == 0) return ci;		work = work->next;		}	return NULL;}#if 0static GList *collection_list_find_link(GList *list, gchar *path){	GList *work = list;	while(work)		{		CollectInfo *ci = work->data;		if (strcmp(ci->path, path) == 0) return work;		work = work->next;		}	return NULL;}static gint collection_list_find_index(GList *list, gchar *path){	gint c = 0;	GList *work = list;	while(work)		{		CollectInfo *ci = work->data;		if (strcmp(ci->path, path) == 0) return c;		work = work->next;		c++;		}	return -1;}#endifGList *collection_list_to_path_list(GList *list){	GList *pathlist = NULL;	GList *work = list;	while (work)		{		CollectInfo *info = work->data;		pathlist = g_list_prepend(pathlist, g_strdup(info->path));		work = work->next;		}	pathlist = g_list_reverse(pathlist);	return pathlist;}CollectWindow *collection_window_find(CollectionData *cd){	GList *work;	work = collection_window_list;	while (work)		{		CollectWindow *cw = work->data;		if (cw->cd == cd) return cw;		work = work->next;		}	return NULL;}CollectWindow *collection_window_find_by_path(const gchar *path){	GList *work;	if (!path) return NULL;	work = collection_window_list;	while (work)		{		CollectWindow *cw = work->data;		if (cw->cd->path && strcmp(cw->cd->path, path) == 0) return cw;		work = work->next;		}	return NULL;}/* *------------------------------------------------------------------- * please use these to actually add/remove stuff *------------------------------------------------------------------- */CollectionData *collection_new(const gchar *path){	CollectionData *cd;	static gint untitled_counter = 0;	cd = g_new0(CollectionData, 1);	collection_list = g_list_append(collection_list, cd);	cd->ref = 1;	/* starts with a ref of 1 */	cd->list = NULL;	cd->sort_method = SORT_NONE;	cd->thumb_loader = NULL;	cd->info_updated_func = NULL;	cd->window_read = FALSE;	cd->window_x = 0;	cd->window_y = 0;	cd->window_w = COLLECT_DEF_WIDTH;	cd->window_h = COLLECT_DEF_HEIGHT;	cd->changed = FALSE;	if (path)		{		cd->path = g_strdup(path);		cd->name = g_strdup(filename_from_path(cd->path));		/* load it */		}	else		{		cd->path = NULL;		if (untitled_counter == 0)			{			cd->name = g_strdup(_("Untitled"));			}		else			{			cd->name = g_strdup_printf(_("Untitled (%d)"), untitled_counter + 1);			}		untitled_counter++;		}	return cd;}void collection_free(CollectionData *cd){	if (!cd) return;	if (debug) printf("collection \"%s\" freed\n", cd->name);	collection_load_stop(cd);	collection_list_free(cd->list);	collection_list = g_list_remove(collection_list, cd);	g_free(cd->path);	g_free(cd->name);	g_free(cd);}void collection_ref(CollectionData *cd){	cd->ref++;	if (debug) printf("collection \"%s\" ref count = %d\n", cd->name, cd->ref);}void collection_unref(CollectionData *cd){	cd->ref--;	if (debug) printf("collection \"%s\" ref count = %d\n", cd->name, cd->ref);	if (cd->ref < 1)		{		collection_free(cd);		}}void collection_path_changed(CollectionData *cd){	collection_window_update_title(collection_window_find(cd));}gint collection_to_number(CollectionData *cd){	return g_list_index(collection_list, cd);}CollectionData *collection_from_number(gint n){	return g_list_nth_data(collection_list, n);}CollectionData *collection_from_dnd_data(const gchar *data, GList **list, GList **info_list){	CollectionData *cd;	gint n;	if (strncmp(data, "COLLECTION:", 11) != 0) return NULL;	n = (gint)strtol(data + 11, NULL, 10);	cd = collection_from_number(n);	if (!cd || (!list && !info_list))		{		return cd;		}	else		{		GList *work = NULL;		GList *infol = NULL;		gint b, e;		b = 0;		while(data[b] != '\0' && data[b] != '\n' ) b++;		b++;		e = b;		while (data[b] != '\0')			{			CollectInfo *info;			while (data[e] != '\n' && data[e] != '\0') e++;			n = (gint)strtol(data + b, NULL, 10);			info = g_list_nth_data(cd->list, n);			if (info && list) work = g_list_append(work, g_strdup(info->path));			if (info && info_list) infol = g_list_append(infol, info);			while (data[e] == '\n') e++;			b = e;			}		if (list) *list = work;		if (info_list) *info_list = infol;		}	return cd;}gchar *collection_info_list_to_dnd_data(CollectionData *cd, GList *list, gint *length){	gchar *uri_text = NULL;	gint total;	GList *work;	gint n;	GList *temp;	gchar *ptr;	n = collection_to_number(cd);	if (!list || n < 0)		{		*length = 0;		return NULL;		}	temp = NULL;	temp = g_list_prepend(temp, g_strdup_printf("COLLECTION:%d\n", n));	work = list;	while(work)		{		n = g_list_index(cd->list, work->data);		if (n >= 0)			{			temp = g_list_prepend(temp, g_strdup_printf("%d\n", n));			}		work = work->next;		}	total = 0;	work = temp;	while(work)		{		total += strlen((gchar *)work->data);		work = work->next;		}	total += 1;	uri_text = g_malloc(total);	ptr = uri_text;	work = g_list_last(temp);	while(work)		{		gchar *text = work->data;		work = work->prev;		strcpy(ptr, text);		ptr += strlen(text);		}	ptr[0] = '\0';	path_list_free(temp);	*length = total;	return uri_text;}gint collection_info_valid(CollectionData *cd, CollectInfo *info){	if (collection_to_number(cd) < 0) return FALSE;	return (g_list_index(cd->list, info) != 0);}CollectInfo *collection_next_by_info(CollectionData *cd, CollectInfo *info){	GList *work;	work = g_list_find(cd->list, info);	if (!work) return NULL;	work = work->next;	if (work) return work->data;	return NULL;}CollectInfo *collection_prev_by_info(CollectionData *cd, CollectInfo *info){	GList *work;	work = g_list_find(cd->list, info);	if (!work) return NULL;	work = work->prev;	if (work) return work->data;	return NULL;}CollectInfo *collection_get_first(CollectionData *cd){	if (cd->list) return cd->list->data;	return NULL;}CollectInfo *collection_get_last(CollectionData *cd){	GList *list;	list = g_list_last(cd->list);	if (list) return list->data;	return NULL;}void collection_set_sort_method(CollectionData *cd, SortType method){	if (!cd) return;	if (cd->sort_method == method) return;	cd->sort_method = method;	cd->list = collection_list_sort(cd->list, cd->sort_method);	if (cd->list) cd->changed = TRUE;	collection_window_refresh(collection_window_find(cd));}void collection_set_update_info_func(CollectionData *cd,				     void (*func)(CollectionData *, CollectInfo *, gpointer), gpointer data){	cd->info_updated_func = func;	cd->info_updated_data = data;}gint collection_add_check(CollectionData *cd, const gchar *path, gint sorted, gint must_exist){	struct stat st;	gint valid;	if (must_exist)		{		valid = (stat_utf8(path, &st) && !S_ISDIR(st.st_mode));		}	else		{		valid = TRUE;		st.st_size = 0;		st.st_mtime = 0;		}	if (valid)		{		CollectInfo *ci;		ci = collection_info_new(path, &st, NULL);		cd->list = collection_list_add(cd->list, ci, sorted ? cd->sort_method : SORT_NONE);		cd->changed = TRUE;		if (!sorted || cd->sort_method == SORT_NONE)			{			collection_window_add(collection_window_find(cd), ci);			}		else			{			collection_window_insert(collection_window_find(cd), ci);			}		}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -