Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include "osl/process.h"
22 : #include "rtl/ustring.hxx"
23 : #include "rtl/string.hxx"
24 : #include "rtl/strbuf.hxx"
25 :
26 : #include "osl/thread.h"
27 : #include "recently_used_file.hxx"
28 :
29 : #include "internal/xml_parser.hxx"
30 : #include "internal/i_xml_parser_event_handler.hxx"
31 :
32 : #include <map>
33 : #include <vector>
34 : #include <algorithm>
35 : #include <functional>
36 : #include <string.h>
37 :
38 : namespace /* private */ {
39 : typedef std::vector<string_t> string_container_t;
40 :
41 : #define TAG_RECENT_FILES "RecentFiles"
42 : #define TAG_RECENT_ITEM "RecentItem"
43 : #define TAG_URI "URI"
44 : #define TAG_MIME_TYPE "Mime-Type"
45 : #define TAG_TIMESTAMP "Timestamp"
46 : #define TAG_PRIVATE "Private"
47 : #define TAG_GROUPS "Groups"
48 : #define TAG_GROUP "Group"
49 :
50 : //------------------------------------------------
51 : // compare two string_t's case insensitive, may also be done
52 : // by specifying special traits for the string type but in this
53 : // case it's easier to do it this way
54 : struct str_icase_cmp :
55 : public std::binary_function<string_t, string_t, bool>
56 : {
57 0 : bool operator() (const string_t& s1, const string_t& s2) const
58 0 : { return (0 == strcasecmp(s1.c_str(), s2.c_str())); }
59 : };
60 :
61 : //------------------------------------------------
62 0 : struct recently_used_item
63 : {
64 0 : recently_used_item() :
65 0 : is_private_(false)
66 0 : {}
67 :
68 0 : recently_used_item(
69 : const string_t& uri,
70 : const string_t& mime_type,
71 : const string_container_t& groups,
72 : bool is_private = false) :
73 : uri_(uri),
74 : mime_type_(mime_type),
75 : is_private_(is_private),
76 0 : groups_(groups)
77 : {
78 0 : timestamp_ = time(NULL);
79 0 : }
80 :
81 0 : void set_uri(const string_t& character)
82 0 : { uri_ = character; }
83 :
84 0 : void set_mime_type(const string_t& character)
85 0 : { mime_type_ = character; }
86 :
87 0 : void set_timestamp(const string_t& character)
88 : {
89 : time_t t;
90 0 : if (sscanf(character.c_str(), "%ld", &t) != 1)
91 0 : timestamp_ = -1;
92 : else
93 0 : timestamp_ = t;
94 0 : }
95 :
96 0 : void set_is_private(SAL_UNUSED_PARAMETER const string_t& /*character*/)
97 0 : { is_private_ = true; }
98 :
99 0 : void set_groups(const string_t& character)
100 0 : { groups_.push_back(character); }
101 :
102 0 : void set_nothing(SAL_UNUSED_PARAMETER const string_t& /*character*/)
103 0 : {}
104 :
105 0 : bool has_groups() const
106 : {
107 0 : return !groups_.empty();
108 : }
109 :
110 0 : bool has_group(const string_t& name) const
111 : {
112 0 : string_container_t::const_iterator iter_end = groups_.end();
113 0 : return (has_groups() &&
114 0 : iter_end != std::find_if(
115 : groups_.begin(), iter_end,
116 0 : std::bind2nd(str_icase_cmp(), name)));
117 : }
118 :
119 0 : void write_xml(const recently_used_file& file) const
120 : {
121 0 : write_xml_start_tag(TAG_RECENT_ITEM, file, true);
122 0 : write_xml_tag(TAG_URI, uri_, file);
123 0 : write_xml_tag(TAG_MIME_TYPE, mime_type_, file);
124 :
125 0 : OString ts = OString::valueOf((sal_sSize)timestamp_);
126 0 : write_xml_tag(TAG_TIMESTAMP, ts.getStr(), file);
127 :
128 0 : if (is_private_)
129 0 : write_xml_tag(TAG_PRIVATE, file);
130 :
131 0 : if (has_groups())
132 : {
133 0 : write_xml_start_tag(TAG_GROUPS, file, true);
134 :
135 0 : string_container_t::const_iterator iter = groups_.begin();
136 0 : string_container_t::const_iterator iter_end = groups_.end();
137 :
138 0 : for ( ; iter != iter_end; ++iter)
139 0 : write_xml_tag(TAG_GROUP, (*iter), file);
140 :
141 0 : write_xml_end_tag(TAG_GROUPS, file);
142 : }
143 0 : write_xml_end_tag(TAG_RECENT_ITEM, file);
144 0 : }
145 :
146 0 : static OString escape_content(const string_t &text)
147 : {
148 0 : OStringBuffer aBuf;
149 0 : for (sal_uInt32 i = 0; i < text.length(); i++)
150 : {
151 0 : switch (text[i])
152 : {
153 0 : case '&': aBuf.append("&"); break;
154 0 : case '<': aBuf.append("<"); break;
155 0 : case '>': aBuf.append(">"); break;
156 0 : case '\'': aBuf.append("'"); break;
157 0 : case '"': aBuf.append("""); break;
158 0 : default: aBuf.append(text[i]); break;
159 : }
160 : }
161 0 : return aBuf.makeStringAndClear();
162 : }
163 :
164 0 : void write_xml_tag(const string_t& name, const string_t& value, const recently_used_file& file) const
165 : {
166 0 : write_xml_start_tag(name, file);
167 0 : OString escaped = escape_content (value);
168 0 : file.write(escaped.getStr(), escaped.getLength());
169 0 : write_xml_end_tag(name, file);
170 0 : }
171 :
172 0 : void write_xml_tag(const string_t& name, const recently_used_file& file) const
173 : {
174 0 : file.write("<", 1);
175 0 : file.write(name.c_str(), name.length());
176 0 : file.write("/>\n", 3);
177 0 : }
178 :
179 0 : void write_xml_start_tag(const string_t& name, const recently_used_file& file, bool linefeed = false) const
180 : {
181 0 : file.write("<", 1);
182 0 : file.write(name.c_str(), name.length());
183 0 : if (linefeed)
184 0 : file.write(">\n", 2);
185 : else
186 0 : file.write(">", 1);
187 0 : }
188 :
189 0 : void write_xml_end_tag(const string_t& name, const recently_used_file& file) const
190 : {
191 0 : file.write("</", 2);
192 0 : file.write(name.c_str(), name.length());
193 0 : file.write(">\n", 2);
194 0 : }
195 :
196 : string_t uri_;
197 : string_t mime_type_;
198 : time_t timestamp_;
199 : bool is_private_;
200 : string_container_t groups_;
201 : };
202 :
203 : typedef std::vector<recently_used_item*> recently_used_item_list_t;
204 : typedef void (recently_used_item::* SET_COMMAND)(const string_t&);
205 :
206 : // thrown if we encounter xml tags that we do not know
207 : class unknown_xml_format_exception {};
208 :
209 0 : class recently_used_file_filter : public i_xml_parser_event_handler
210 : {
211 : public:
212 0 : recently_used_file_filter(recently_used_item_list_t& item_list) :
213 : item_(NULL),
214 0 : item_list_(item_list)
215 : {
216 0 : named_command_map_[TAG_RECENT_FILES] = &recently_used_item::set_nothing;
217 0 : named_command_map_[TAG_RECENT_ITEM] = &recently_used_item::set_nothing;
218 0 : named_command_map_[TAG_URI] = &recently_used_item::set_uri;
219 0 : named_command_map_[TAG_MIME_TYPE] = &recently_used_item::set_mime_type;
220 0 : named_command_map_[TAG_TIMESTAMP] = &recently_used_item::set_timestamp;
221 0 : named_command_map_[TAG_PRIVATE] = &recently_used_item::set_is_private;
222 0 : named_command_map_[TAG_GROUPS] = &recently_used_item::set_nothing;
223 0 : named_command_map_[TAG_GROUP] = &recently_used_item::set_groups;
224 0 : }
225 :
226 0 : virtual void start_element(
227 : const string_t& /*raw_name*/,
228 : const string_t& local_name,
229 : const xml_tag_attribute_container_t& /*attributes*/)
230 : {
231 0 : if ((local_name == TAG_RECENT_ITEM) && (NULL == item_))
232 0 : item_ = new recently_used_item;
233 0 : }
234 :
235 0 : virtual void end_element(const string_t& /*raw_name*/, const string_t& local_name)
236 : {
237 : // check for end tags w/o start tag
238 0 : if( local_name != TAG_RECENT_FILES && NULL == item_ )
239 0 : return; // will result in an XML parser error anyway
240 :
241 0 : if (named_command_map_.find(local_name) != named_command_map_.end())
242 0 : (item_->*named_command_map_[local_name])(current_element_);
243 : else
244 : {
245 0 : delete item_;
246 0 : throw unknown_xml_format_exception();
247 : }
248 :
249 0 : if (local_name == TAG_RECENT_ITEM)
250 : {
251 0 : item_list_.push_back(item_);
252 0 : item_ = NULL;
253 : }
254 0 : current_element_.clear();
255 : }
256 :
257 0 : virtual void characters(const string_t& character)
258 : {
259 0 : if (character != "\n")
260 0 : current_element_ += character;
261 0 : }
262 :
263 0 : virtual void start_document() {}
264 0 : virtual void end_document() {}
265 :
266 0 : virtual void ignore_whitespace(const string_t& /*whitespaces*/)
267 0 : {}
268 :
269 0 : virtual void processing_instruction(
270 : const string_t& /*target*/, const string_t& /*data*/)
271 0 : {}
272 :
273 0 : virtual void comment(const string_t& /*comment*/)
274 0 : {}
275 : private:
276 : recently_used_item* item_;
277 : std::map<string_t, SET_COMMAND> named_command_map_;
278 : string_t current_element_;
279 : recently_used_item_list_t& item_list_;
280 : private:
281 : recently_used_file_filter(const recently_used_file_filter&);
282 : recently_used_file_filter& operator=(const recently_used_file_filter&);
283 : };
284 :
285 : //------------------------------------------------
286 0 : void read_recently_used_items(
287 : recently_used_file& file,
288 : recently_used_item_list_t& item_list)
289 : {
290 0 : xml_parser xparser;
291 0 : recently_used_file_filter ruff(item_list);
292 :
293 0 : xparser.set_document_handler(&ruff);
294 :
295 : char buff[16384];
296 0 : while (!file.eof())
297 : {
298 0 : if (size_t length = file.read(buff, sizeof(buff)))
299 0 : xparser.parse(buff, length, file.eof());
300 0 : }
301 0 : }
302 :
303 : //------------------------------------------------
304 : // The file ~/.recently_used shall not contain more than 500
305 : // entries (see www.freedesktop.org)
306 : const int MAX_RECENTLY_USED_ITEMS = 500;
307 :
308 : class recent_item_writer
309 : {
310 : public:
311 0 : recent_item_writer(
312 : recently_used_file& file,
313 : int max_items_to_write = MAX_RECENTLY_USED_ITEMS) :
314 : file_(file),
315 : max_items_to_write_(max_items_to_write),
316 0 : items_written_(0)
317 0 : {}
318 :
319 0 : void operator() (const recently_used_item* item)
320 : {
321 0 : if (items_written_++ < max_items_to_write_)
322 0 : item->write_xml(file_);
323 0 : }
324 : private:
325 : recently_used_file& file_;
326 : int max_items_to_write_;
327 : int items_written_;
328 : };
329 :
330 : //------------------------------------------------
331 : const char* XML_HEADER = "<?xml version=\"1.0\"?>\n<RecentFiles>\n";
332 : const char* XML_FOOTER = "</RecentFiles>";
333 :
334 : //------------------------------------------------
335 : // assumes that the list is ordered decreasing
336 0 : void write_recently_used_items(
337 : recently_used_file& file,
338 : recently_used_item_list_t& item_list)
339 : {
340 0 : if (!item_list.empty())
341 : {
342 0 : file.truncate();
343 0 : file.reset();
344 :
345 0 : file.write(XML_HEADER, strlen(XML_HEADER));
346 :
347 : std::for_each(
348 : item_list.begin(),
349 : item_list.end(),
350 0 : recent_item_writer(file));
351 :
352 0 : file.write(XML_FOOTER, strlen(XML_FOOTER));
353 : }
354 0 : }
355 :
356 : //------------------------------------------------
357 : struct delete_recently_used_item
358 : {
359 0 : void operator() (const recently_used_item* item) const
360 0 : { delete item; }
361 : };
362 :
363 : //------------------------------------------------
364 0 : void recently_used_item_list_clear(recently_used_item_list_t& item_list)
365 : {
366 : std::for_each(
367 : item_list.begin(),
368 : item_list.end(),
369 0 : delete_recently_used_item());
370 0 : item_list.clear();
371 0 : }
372 :
373 : //------------------------------------------------
374 0 : class find_item_predicate
375 : {
376 : public:
377 0 : find_item_predicate(const string_t& uri) :
378 0 : uri_(uri)
379 0 : {}
380 :
381 0 : bool operator() (const recently_used_item* item) const
382 0 : { return (item->uri_ == uri_); }
383 : private:
384 : string_t uri_;
385 : };
386 :
387 : //------------------------------------------------
388 : struct greater_recently_used_item
389 : {
390 0 : bool operator ()(const recently_used_item* lhs, const recently_used_item* rhs) const
391 0 : { return (lhs->timestamp_ > rhs->timestamp_); }
392 : };
393 :
394 : //------------------------------------------------
395 : const char* GROUP_OOO = "openoffice.org";
396 : const char* GROUP_STAR_OFFICE = "staroffice";
397 : const char* GROUP_STAR_SUITE = "starsuite";
398 :
399 : //------------------------------------------------
400 0 : void recently_used_item_list_add(
401 : recently_used_item_list_t& item_list, const OUString& file_url, const OUString& mime_type)
402 : {
403 0 : OString f = OUStringToOString(file_url, RTL_TEXTENCODING_UTF8);
404 :
405 : recently_used_item_list_t::iterator iter =
406 : std::find_if(
407 : item_list.begin(),
408 : item_list.end(),
409 0 : find_item_predicate(f.getStr()));
410 :
411 0 : if (iter != item_list.end())
412 : {
413 0 : (*iter)->timestamp_ = time(NULL);
414 :
415 0 : if (!(*iter)->has_group(GROUP_OOO))
416 0 : (*iter)->groups_.push_back(GROUP_OOO);
417 0 : if (!(*iter)->has_group(GROUP_STAR_OFFICE))
418 0 : (*iter)->groups_.push_back(GROUP_STAR_OFFICE);
419 0 : if (!(*iter)->has_group(GROUP_STAR_SUITE))
420 0 : (*iter)->groups_.push_back(GROUP_STAR_SUITE);
421 : }
422 : else
423 : {
424 0 : string_container_t groups;
425 0 : groups.push_back(GROUP_OOO);
426 0 : groups.push_back(GROUP_STAR_OFFICE);
427 0 : groups.push_back(GROUP_STAR_SUITE);
428 :
429 0 : string_t uri(f.getStr());
430 0 : string_t mimetype(OUStringToOString(mime_type, osl_getThreadTextEncoding()).getStr());
431 :
432 0 : if (mimetype.length() == 0)
433 0 : mimetype = "application/octet-stream";
434 :
435 0 : item_list.push_back(new recently_used_item(uri, mimetype, groups));
436 : }
437 :
438 : // sort decreasing after the timestamp
439 : // so that the newest items appear first
440 : std::sort(
441 : item_list.begin(),
442 : item_list.end(),
443 0 : greater_recently_used_item());
444 0 : }
445 :
446 : //------------------------------------------------
447 : struct cleanup_guard
448 : {
449 0 : cleanup_guard(recently_used_item_list_t& item_list) :
450 0 : item_list_(item_list)
451 0 : {}
452 0 : ~cleanup_guard()
453 0 : { recently_used_item_list_clear(item_list_); }
454 :
455 : recently_used_item_list_t& item_list_;
456 : };
457 :
458 : } // namespace private
459 :
460 : /*
461 : example (see http::www.freedesktop.org):
462 : <?xml version="1.0"?>
463 : <RecentFiles>
464 : <RecentItem>
465 : <URI>file:///home/federico/gedit.txt</URI>
466 : <Mime-Type>text/plain</Mime-Type>
467 : <Timestamp>1046485966</Timestamp>
468 : <Groups>
469 : <Group>gedit</Group>
470 : </Groups>
471 : </RecentItem>
472 : <RecentItem>
473 : <URI>file:///home/federico/gedit-2.2.0.tar.bz2</URI>
474 : <Mime-Type>application/x-bzip</Mime-Type>
475 : <Timestamp>1046209851</Timestamp>
476 : <Private/>
477 : <Groups>
478 : </Groups>
479 : </RecentItem>
480 : </RecentFiles>
481 : */
482 :
483 : extern "C" SAL_DLLPUBLIC_EXPORT
484 0 : void add_to_recently_used_file_list(const OUString& file_url,
485 : const OUString& mime_type)
486 : {
487 : try
488 : {
489 0 : recently_used_file ruf;
490 0 : recently_used_item_list_t item_list;
491 0 : cleanup_guard guard(item_list);
492 :
493 0 : read_recently_used_items(ruf, item_list);
494 0 : recently_used_item_list_add(item_list, file_url, mime_type);
495 0 : write_recently_used_items(ruf, item_list);
496 : }
497 0 : catch(const char* ex)
498 : {
499 : OSL_FAIL(ex);
500 : }
501 0 : catch(const xml_parser_exception&)
502 : {
503 : OSL_FAIL("XML parser error");
504 : }
505 0 : catch(const unknown_xml_format_exception&)
506 : {
507 : OSL_FAIL("XML format unknown");
508 : }
509 0 : }
510 :
511 :
512 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|