author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 04 Oct 2010 01:19:32 +0300 | |
changeset 37 | 758a864f9613 |
parent 18 | 2f34d5167611 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the Qt Linguist of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "recentfiles.h" |
|
43 |
#include "globals.h" |
|
44 |
||
45 |
#include <QtCore/QDebug> |
|
46 |
#include <QtCore/QFileInfo> |
|
47 |
#include <QtCore/QSettings> |
|
48 |
#include <QtCore/QString> |
|
49 |
#include <QtCore/QStringList> |
|
50 |
||
51 |
QT_BEGIN_NAMESPACE |
|
52 |
||
53 |
static QString configKey() |
|
54 |
{ |
|
55 |
return settingPath("RecentlyOpenedFiles"); |
|
56 |
} |
|
57 |
||
58 |
||
59 |
RecentFiles::RecentFiles(const int maxEntries) |
|
60 |
: m_groupOpen(false), |
|
61 |
m_clone1st(false), |
|
62 |
m_maxEntries(maxEntries) |
|
63 |
{ |
|
64 |
m_timer.setSingleShot(true); |
|
65 |
m_timer.setInterval(3 * 60 * 1000); |
|
66 |
connect(&m_timer, SIGNAL(timeout()), SLOT(closeGroup())); |
|
67 |
} |
|
68 |
||
69 |
/* |
|
70 |
* The logic is as follows: |
|
71 |
* - The most recent (i.e., topmost) item can be open ("in flux") |
|
72 |
* - The item is closed by either a timeout (3 min or so) or a |
|
73 |
* "terminal action" (e.g., closing all files) |
|
74 |
* - While the item is open, modifications to the set of open files |
|
75 |
* will modify that item instead of creating new items |
|
76 |
* - If the open item is modified to be equal to an existing item, |
|
77 |
* the existing item is deleted, but will be re-created when the |
|
78 |
* open item is modified even further |
|
79 |
* Cases (actions in parentheses are no-ops): |
|
80 |
* - identical to top item => (do nothing) |
|
81 |
* - closed, new item => insert at top, (clear marker) |
|
82 |
* - closed, existing item => move to top, mark for cloning |
|
83 |
* - open, new item, not marked => replace top, (clear marker) |
|
84 |
* - open, new item, marked => insert at top, clear marker |
|
85 |
* - open, existing item, not marked => replace top, delete copy, mark for cloning |
|
86 |
* - open, existing item, marked => insert at top, delete copy, (mark for cloning) |
|
87 |
* - closing clears marker |
|
88 |
*/ |
|
89 |
void RecentFiles::addFiles(const QStringList &names) |
|
90 |
{ |
|
91 |
if (m_strLists.isEmpty() || names != m_strLists.first()) { |
|
92 |
if (m_groupOpen && !m_clone1st) |
|
93 |
// Group being open implies at least one item in the list |
|
94 |
m_strLists.removeFirst(); |
|
95 |
m_groupOpen = true; |
|
96 |
||
97 |
// We do *not* sort the actual entries, as that would destroy the user's |
|
98 |
// chosen arrangement. However, we do the searching on sorted lists, so |
|
99 |
// we throw out (probably) obsolete arrangements. |
|
100 |
QList<QStringList> sortedLists = m_strLists; |
|
101 |
for (int i = 0; i < sortedLists.size(); ++i) |
|
102 |
sortedLists[i].sort(); |
|
103 |
QStringList sortedNames = names; |
|
104 |
sortedNames.sort(); |
|
105 |
||
106 |
int index = sortedLists.indexOf(sortedNames); |
|
107 |
if (index >= 0) { |
|
108 |
m_strLists.removeAt(index); |
|
109 |
m_clone1st = true; |
|
110 |
} else { |
|
111 |
if (m_strLists.count() >= m_maxEntries) |
|
112 |
m_strLists.removeLast(); |
|
113 |
m_clone1st = false; |
|
114 |
} |
|
115 |
m_strLists.prepend(names); |
|
116 |
} |
|
117 |
m_timer.start(); |
|
118 |
} |
|
119 |
||
120 |
void RecentFiles::closeGroup() |
|
121 |
{ |
|
122 |
m_timer.stop(); |
|
123 |
m_groupOpen = false; |
|
124 |
} |
|
125 |
||
126 |
void RecentFiles::readConfig() |
|
127 |
{ |
|
128 |
m_strLists.clear(); |
|
129 |
QVariant val = QSettings().value(configKey()); |
|
130 |
if (val.type() == QVariant::StringList) // Backwards compat to Qt < 4.5 |
|
131 |
foreach (const QString &s, val.toStringList()) |
|
132 |
m_strLists << QStringList(QFileInfo(s).canonicalFilePath()); |
|
133 |
else |
|
134 |
foreach (const QVariant &v, val.toList()) |
|
135 |
m_strLists << v.toStringList(); |
|
136 |
} |
|
137 |
||
138 |
void RecentFiles::writeConfig() const |
|
139 |
{ |
|
140 |
QList<QVariant> vals; |
|
141 |
foreach (const QStringList &sl, m_strLists) |
|
142 |
vals << sl; |
|
143 |
QSettings().setValue(configKey(), vals); |
|
144 |
} |
|
145 |
||
146 |
QT_END_NAMESPACE |