File: pinot-label.cpp

package info (click to toggle)
pinot 1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,308 kB
  • sloc: cpp: 39,057; sh: 10,481; ansic: 4,174; makefile: 612; xml: 372
file content (250 lines) | stat: -rw-r--r-- 5,698 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
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
/*
 *  Copyright 2007-2009 Fabrice Colin
 *
 *  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
 
#include <getopt.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <set>

#include "config.h"
#include "StringManip.h"
#include "MIMEScanner.h"
#include "Url.h"
#include "DBusIndex.h"

using namespace std;

static struct option g_longOptions[] = {
	{"get", 0, 0, 'g'},
	{"help", 0, 0, 'h'},
	{"list", 0, 0, 'l'},
	{"set", 1, 0, 's'},
	{"version", 0, 0, 'v'},
	{0, 0, 0, 0}
};


static void printLabels(const set<string> &labels, const string &fileName)
{
	if (fileName.empty() == false)
	{
		clog << fileName << endl;
	}
	clog << "Labels: ";

	for (set<string>::const_iterator labelIter = labels.begin();
		labelIter != labels.end(); ++labelIter)
	{
		if (labelIter->substr(0, 2) == "X-")
		{
			continue;
		}
		clog << "[" << Url::escapeUrl(*labelIter) << "]";
	}
	clog << endl;
}

static void printHelp(void)
{
	// Help
	clog << "pinot-label - Label files from the command-line\n\n"
		<< "Usage: pinot-label [OPTIONS] [FILES]\n\n"
		<< "Options:\n"
		<< "  -g, --get                 get the labels list for the given file\n"
		<< "  -h, --help                display this help and exit\n"
		<< "  -l, --list                list known labels\n"
		<< "  -s, --set                 set labels on the given file\n"
		<< "  -v, --version             output version information and exit\n\n";
	clog << "Examples:\n"
		<< "pinot-label --get /home/fabrice/Documents/Bozo.txt\n\n"
		<< "pinot-label --list\n\n"
		<< "pinot-label --set \"[Clowns][Fun][My Hero]\" /home/fabrice/Documents/Bozo.txt\n\n"
		<< "Report bugs to " << PACKAGE_BUGREPORT << endl;
}

int main(int argc, char **argv)
{
	set<string> labels;
	string labelsString;
	int longOptionIndex = 0;
	unsigned int docId = 0;
	int minArgNum = 1;
	bool getLabels = false, getDocumentLabels = false, setDocumentLabels = false, success = false;

	// Look at the options
	int optionChar = getopt_long(argc, argv, "ghls:v", g_longOptions, &longOptionIndex);
	while (optionChar != -1)
	{
		set<string> engines;

		switch (optionChar)
		{
			case 'g':
				getDocumentLabels = true;
				break;
			case 'h':
				printHelp();
				return EXIT_SUCCESS;
			case 'l':
				minArgNum = 0;
				getLabels = true;
				break;
			case 's':
				setDocumentLabels = true;
				if (optarg != NULL)
				{
					labelsString = optarg;
				}
				break;
			case 'v':
				clog << "pinot-label - " << PACKAGE_STRING << "\n\n"
					<< "This is free software.  You may redistribute copies of it under the terms of\n"
					<< "the GNU General Public License <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>.\n"
					<< "There is NO WARRANTY, to the extent permitted by law." << endl;
				return EXIT_SUCCESS;
			default:
				return EXIT_FAILURE;
		}

		// Next option
		optionChar = getopt_long(argc, argv, "ghls:v", g_longOptions, &longOptionIndex);
	}

	if (argc == 1)
	{
		printHelp();
		return EXIT_SUCCESS;
	}

	if ((argc < 2) ||
		(argc - optind < minArgNum))
	{
		clog << "Not enough parameters" << endl;
		return EXIT_FAILURE;
	}

	if ((setDocumentLabels == true) &&
		(labelsString.empty() == true))
	{
		clog << "Incorrect parameters" << endl;
		return EXIT_FAILURE;
	}

	// Initialize GType
	g_type_init();

	MIMEScanner::initialize("", "");

	// We need a pure DBusIndex object
	DBusIndex index(NULL);

	if (getLabels == true)
	{
		if (index.getLabels(labels) == true)
		{
			printLabels(labels, "");

			success = true;
		}
	}

	while (optind < argc)
	{
		string fileParam(argv[optind]);
		Url thisUrl(fileParam, "");

		// Rewrite it as a local URL
		string urlParam(thisUrl.getProtocol());
		urlParam += "://";
		urlParam += thisUrl.getLocation();
		if (thisUrl.getFile().empty() == false)
		{
			urlParam += "/";
			urlParam += thisUrl.getFile();
		}
#ifdef DEBUG
		clog << "URL rewritten to " << urlParam << endl;
#endif

		if ((getDocumentLabels == true) ||
			(setDocumentLabels == true))
		{
			docId = index.hasDocument(urlParam);
			if (docId == 0)
			{
				clog << fileParam << " is not indexed" << endl;
				success = false;

				// Next
				++optind;
				continue;
			}
		}

		if (getDocumentLabels == true)
		{
			labels.clear();

			if (index.getDocumentLabels(docId, labels) == true)
			{
				printLabels(labels, fileParam);

				success = true;
			}
		}

		if (setDocumentLabels == true)
		{
			string::size_type endPos = 0;
			string label(StringManip::extractField(labelsString, "[", "]", endPos));

			labels.clear();

			// Parse labels
			while (label.empty() == false)
			{
				labels.insert(Url::unescapeUrl(label));

				if (endPos == string::npos)
				{
					break;
				}
				label = StringManip::extractField(labelsString, "[", "]", endPos);
			}

#ifdef DEBUG
			printLabels(labels, fileParam);
#endif
			success = index.setDocumentLabels(docId, labels);
		}

		// Next
		++optind;
	}

	MIMEScanner::shutdown();

	// Did whatever operation we carried out succeed ?
	if (success == true)
	{
		return EXIT_SUCCESS;
	}

	return EXIT_FAILURE;
}