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