File: libsqueeze.c

package info (click to toggle)
squeeze 0.2.3-8
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,292 kB
  • ctags: 1,278
  • sloc: ansic: 12,933; sh: 9,240; xml: 617; makefile: 191
file content (171 lines) | stat: -rw-r--r-- 4,130 bytes parent folder | download | duplicates (3)
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
/*
 *  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 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.  
 */


#include <config.h>
#include <string.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <glib-object.h>
#include <thunar-vfs/thunar-vfs.h>

#include "libsqueeze.h"
#include "libsqueeze/libsqueeze-module.h"
#include "libsqueeze/libsqueeze-vfs-mime.h"
#include "libsqueeze/archive-iter.h"
#include "libsqueeze/archive-command.h"
#include "libsqueeze/archive.h"
#include "libsqueeze/command-builder.h"
#include "libsqueeze/command-builder-zip.h"
#include "libsqueeze/command-builder-rar.h"
#include "libsqueeze/command-builder-compr.h"
#include "libsqueeze/command-builder-gnu-tar.h"

#include "vfs-mime.h"

#include "internals.h"

static gint
lsq_lookup_mime(gconstpointer a, gconstpointer b);

void
lsq_init()
{
	LSQCommandBuilder *builder = NULL;
	gchar *current_dir = g_get_current_dir();

	lsq_mime_database = thunar_vfs_mime_database_get_default();

	builder = lsq_command_builder_zip_new();
	if(builder)
		lsq_command_builder_register(builder);

	builder = lsq_command_builder_gnu_tar_new();
	if(builder)
		lsq_command_builder_register(builder);

	builder = lsq_command_builder_rar_new();
	if(builder)
		lsq_command_builder_register(builder);

	builder = lsq_command_builder_compr_new();
	if(builder)
		lsq_command_builder_register(builder);

	lsq_relative_base_path = thunar_vfs_path_new(current_dir, NULL);
	lsq_opened_archive_list = NULL;
	g_free(current_dir);
}

void
lsq_shutdown()
{
	g_slist_foreach(lsq_opened_archive_list, (GFunc)lsq_close_archive, NULL);

	g_object_unref(lsq_mime_database);
	thunar_vfs_path_unref(lsq_relative_base_path);
}

/*
 * XAArchive* lsq_new_archive(gchar *path, LSQArchiveType type, gboolean overwrite)
 *
 */
gint
lsq_new_archive(gchar *path, gboolean overwrite, gchar *mime, LSQArchive **lp_archive)
{
	if(overwrite)
		g_unlink(path);

	if(g_file_test(path, G_FILE_TEST_EXISTS))
	{
		(*lp_archive) = NULL;
		return 1;
	}

	LSQArchive *archive = lsq_archive_new(path, mime);
	(*lp_archive) = archive;
	if(!archive)
		return 1;
	return 0;
}

/*
 *
 * XAArchive* lsq_open_archive(gchar *path)
 *
 */
gint
lsq_open_archive(gchar *path, LSQArchive **lp_archive)
{
	if(!g_file_test(path, G_FILE_TEST_EXISTS))
	{
		(*lp_archive) = NULL;
		return 1;
	}

	LSQArchive *archive = lsq_opened_archive_get_archive(path);
	if(!archive)
	{
		archive = lsq_archive_new(path, NULL);
		if(archive)
			lsq_opened_archive_list = g_slist_prepend(lsq_opened_archive_list, archive);
	}
	(*lp_archive) = archive;
	if(!archive)
		return 1;
	return 0;
}


GSList *
lsq_get_supported_mime_types(LSQSupportTypes types)
{
	GSList *m_types = g_slist_copy(lsq_mime_info_list);
	if(types &= LSQ_SUPPORT_ADD)
	{
		GSList *_types = m_types;
		while(_types)
		{
			LSQArchiveMime *mime = _types->data;
			LSQCommandBuilder *builder = mime->command_builders->data;
			if(!builder->build_add)
				m_types = g_slist_remove(m_types, mime);
			_types = g_slist_next(_types);
		}
	}
	
	return m_types;
}

static gint
lsq_lookup_mime(gconstpointer a, gconstpointer b)
{
	return !thunar_vfs_mime_info_equal((((LSQArchiveMime *)a)->mime_info), b);
}

gboolean
lsq_is_supported(const gchar *filename)
{
	ThunarVfsMimeInfo *mime_info = thunar_vfs_mime_database_get_info_for_name(lsq_mime_database, filename);
	GSList *result = g_slist_find_custom(lsq_mime_info_list, mime_info, lsq_lookup_mime);
	thunar_vfs_mime_info_unref(mime_info);
	if(result)
	{
		return TRUE;
	}
	return FALSE;
}