1 /****************************************************************************
2 **
3 ** This file is part of the LibreCAD project, a 2D CAD program
4 **
5 ** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
6 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
7 **
8 **
9 ** This file may be distributed and/or modified under the terms of the
10 ** GNU General Public License version 2 as published by the Free Software
11 ** Foundation and appearing in the file gpl-2.0.txt included in the
12 ** packaging of this file.
13 **
14 ** This program is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ** GNU General Public License for more details.
18 **
19 ** You should have received a copy of the GNU General Public License
20 ** along with this program; if not, write to the Free Software
21 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 **
23 ** This copyright notice MUST APPEAR in all copies of the script!
24 **
25 **********************************************************************/
26 #include <QFileInfo>
27 #include <QAction>
28 #include <QActionGroup>
29 #include <QMenu>
30 #include "qg_recentfiles.h"
31 
32 #include "rs_debug.h"
33 #include "rs_settings.h"
34 
35 /**
36  * Constructor
37  * @param number Number of files that can be stored in the list at maximum
38  */
QG_RecentFiles(QObject * parent,int number)39 QG_RecentFiles::QG_RecentFiles(QObject* parent, int number)
40     : QObject(parent)
41     , number(number)
42 {}
43 
~QG_RecentFiles()44 QG_RecentFiles::~QG_RecentFiles()
45 {
46 	RS_SETTINGS->beginGroup("/RecentFiles");
47 	for (int i=0; i<count(); ++i) {
48 		RS_SETTINGS->writeEntry(QString("/File") + QString::number(i+1), get(i));
49 	}
50 	RS_SETTINGS->endGroup();
51 }
52 
53 /**
54  * Adds a file to the list of recently loaded files if
55  * it's not already in the list.
56  */
add(const QString & filename)57 void QG_RecentFiles::add(const QString& filename) {
58     RS_DEBUG->print("QG_RecentFiles::add");
59 	if(filename.size()>2048){
60 		RS_DEBUG->print(RS_Debug::D_ERROR, "QG_RecentFiles::add filename too long at %d\n", filename.size());
61 		return;
62 	}
63 
64     // is the file already in the list?
65     int i0=files.indexOf(filename);
66     if (i0>=0) {
67 		if (i0+1==files.size()) return; //do nothing, file already being the last in list
68         //move the i0 to the last
69 		files.erase(files.begin() + i0);
70 		files.push_back(filename);
71         return;
72     }
73 
74     // append
75     //files.push_back(filename);
76     files.append(filename);
77 	if(files.size() > number)
78 		files.erase(files.begin(), files.begin() + files.size() - number);
79 	RS_DEBUG->print("QG_RecentFiles::add: OK");
80 }
81 
82 
get(int i) const83 QString QG_RecentFiles::get(int i) const{
84 	if (i<files.size()) {
85 		return files[i];
86 	} else {
87 		return QString("");
88 	}
89 }
90 
count() const91 int QG_RecentFiles::count() const {
92 	return files.count();
93 }
94 
95 /** @return number of files that can be stored in the list at maximum */
getNumber() const96 int QG_RecentFiles::getNumber() const {
97 	return number;
98 }
99 
indexOf(const QString & filename) const100 int QG_RecentFiles::indexOf(const QString& filename) const{
101 	return files.indexOf(filename) ;
102 }
103 
addFiles(QMenu * file_menu)104 void QG_RecentFiles::addFiles(QMenu* file_menu)
105 {
106     RS_DEBUG->print("QG_RecentFiles::addFiles()");
107 
108     RS_SETTINGS->beginGroup("/RecentFiles");
109     for (int i=0; i<number; ++i)
110     {
111         QString filename = RS_SETTINGS->readEntry(QString("/File") +
112                            QString::number(i+1));
113         if (QFileInfo(filename).exists()) add(filename);
114     }
115     RS_SETTINGS->endGroup();
116 
117     QActionGroup* a_group = new QActionGroup(this);
118     connect(a_group, SIGNAL(triggered(QAction*)),
119             parent(), SLOT(slotFileOpenRecent(QAction*)));
120 
121     for (int i = 0; i < number; ++i)
122     {
123         recentFilesAction.push_back(new QAction(a_group));
124         QAction* a=recentFilesAction.back();
125         a->setVisible(false);
126         file_menu->addAction(a);
127     }
128     if (count()>0) {
129         updateRecentFilesMenu();
130     }
131 }
132 
133 
updateRecentFilesMenu()134 void QG_RecentFiles::updateRecentFilesMenu() {
135 	RS_DEBUG->print("QG_RecentFiles::updateRecentFilesMenu(): begin\n");
136 
137 	RS_DEBUG->print("Updating recent file menu...");
138 	int numRecentFiles = std::min(count(), getNumber());
139 
140 	for (int i = 0; i < numRecentFiles; ++i) {
141 		//oldest on top
142 //        QString text = tr("&%1 %2").arg(i + 1).arg(recentFiles->get(i));
143 		//newest on top
144 
145         auto file_path = get(numRecentFiles-i-1);
146         if (file_path.length() > 128)
147             file_path = "..." + file_path.right(128);
148         QString const& text = tr("&%1 %2").arg(i + 1).arg(file_path);
149 
150 		recentFilesAction[i]->setText(text);
151 		//newest on top
152 		recentFilesAction[i]->setData(get(numRecentFiles-i-1));
153 		recentFilesAction[i]->setVisible(true);
154 	}
155 	for (int j = numRecentFiles; j < getNumber(); ++j)
156 		recentFilesAction[j]->setVisible(false);
157 	RS_DEBUG->print("QG_RecentFiles::updateRecentFilesMenu(): ok\n");
158 }
159 
160 
161