1 /*
2 For general Scribus (>=1.3.2) copyright and licensing information please refer
3 to the COPYING file provided with the program. Following this notice may exist
4 a copyright and/or license notice that predates the release of Scribus 1.3.2
5 for which a new license (GPL+exception) is in place.
6 */
7 /**************************************************************************
8 *   Copyright (C) 2016 by Franz Schmid                                    *
9 *   franz.schmid@altmuehlnet.de                                           *
10 *                                                                         *
11 *   This program is free software; you can redistribute it and/or modify  *
12 *   it under the terms of the GNU General Public License as published by  *
13 *   the Free Software Foundation; either version 2 of the License, or     *
14 *   (at your option) any later version.                                   *
15 *                                                                         *
16 *   This program is distributed in the hope that it will be useful,       *
17 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
18 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
19 *   GNU General Public License for more details.                          *
20 *                                                                         *
21 *   You should have received a copy of the GNU General Public License     *
22 *   along with this program; if not, write to the                         *
23 *   Free Software Foundation, Inc.,                                       *
24 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
25 ***************************************************************************/
26 
27 #include "recoverdialog.h"
28 #include "iconmanager.h"
29 #include <QList>
30 #include <QTableWidget>
31 #include <QTableWidgetItem>
32 #include <QCheckBox>
33 #include <QFileInfo>
34 
RecoverDialog(QWidget * parent,const QStringList & files)35 RecoverDialog::RecoverDialog(QWidget* parent, const QStringList& files) : QDialog(parent)
36 {
37 	setupUi(this);
38 	setModal(true);
39 	setWindowIcon(IconManager::instance().loadPixmap("AppIcon.png"));
40 	m_files = files;
41 	recoverFiles.clear();
42 
43 	QHeaderView *header = filesList->horizontalHeader();
44 	header->setStretchLastSection(true);
45 	header->setSectionsClickable(true );
46 	header->setSectionsMovable( false );
47 	header->setSectionResizeMode(QHeaderView::Fixed);
48 	header->setHighlightSections(false);
49 
50 	filesList->setColumnWidth(0, 24);
51 	filesList->horizontalHeaderItem(0)->setIcon(style()->standardIcon(QStyle::SP_DialogApplyButton));
52 	filesList->horizontalHeaderItem(0)->setToolTip("<qt>" + tr("Click here to toggle your choice") + "</qt>" );
53 	filesList->horizontalHeaderItem(3)->setToolTip("<qt>" + tr("Autosaved files are automatically saved versions of a document that was opened in a previous Scribus session.") + "<br>" + "Emergency files are files that Scribus was able to salvage after a crash." + "</qt>" );
54 
55 	updateFilesTable();
56 	toggleAllfromHeader(0);
57 
58 	connect(buttonRecover, SIGNAL(clicked()), this, SLOT(doRecover()));
59 	connect(buttonRemove, SIGNAL(clicked()), this, SLOT(doRemove()));
60 	connect(header, SIGNAL(sectionClicked(int)), this, SLOT(toggleAllfromHeader(int)));
61 }
62 
toggleAllfromHeader(int col)63 void RecoverDialog::toggleAllfromHeader(int col)
64 {
65 	if (col != 0)
66 		return;
67 	for (int a = 0; a < filesList->rowCount(); a++)
68 	{
69 		((QCheckBox*)(filesList->cellWidget(a, 0)))->toggle();
70 	}
71 	handleItemClick();
72 }
73 
handleItemClick()74 void RecoverDialog::handleItemClick()
75 {
76 	bool found = false;
77 	for (int a = 0; a < filesList->rowCount(); a++)
78 	{
79 		if (((QCheckBox*)(filesList->cellWidget(a, 0)))->isChecked())
80 		{
81 			found = true;
82 			break;
83 		}
84 
85 	}
86 	buttonRecover->setEnabled(found);
87 	buttonRemove->setEnabled(found);
88 }
89 
doRecover()90 void RecoverDialog::doRecover()
91 {
92 	for (int a = 0; a < filesList->rowCount(); a++)
93 	{
94 		if (((QCheckBox*)(filesList->cellWidget(a, 0)))->isChecked())
95 		{
96 			recoverFiles.append(m_files[a]);
97 			QString nName = m_files[a];
98 			if (nName.contains("emergency"))
99 				nName.replace("emergency", tr("(recovered)"));
100 			if (nName.contains("autosave"))
101 				nName.replace("autosave", tr("(recovered)"));
102 			recoverNames.append(nName);
103 		}
104 	}
105 	accept();
106 }
107 
doRemove()108 void RecoverDialog::doRemove()
109 {
110 	QStringList filesToRemove;
111 	filesToRemove.clear();
112 	for (int a = 0; a < filesList->rowCount(); a++)
113 	{
114 		if (((QCheckBox*)(filesList->cellWidget(a, 0)))->isChecked())
115 		{
116 			QFile::remove(m_files[a]);
117 			filesToRemove.append(m_files[a]);
118 		}
119 	}
120 	for (int a = 0; a < filesToRemove.count(); a++)
121 	{
122 		m_files.removeAll(filesToRemove[a]);
123 	}
124 	if (filesList->rowCount() == filesToRemove.count())
125 		accept();
126 	else
127 		updateFilesTable();
128 }
129 
updateFilesTable()130 void RecoverDialog::updateFilesTable()
131 {
132 	filesList->clearContents();
133 	filesList->setRowCount(m_files.count());
134 	buttonRemove->setEnabled(false);
135 	buttonRecover->setEnabled(false);
136 	for (int a = 0; a < m_files.count(); a++)
137 	{
138 		QFileInfo fi(m_files[a]);
139 		QString name = fi.baseName();
140 		QStringList parts;
141 		parts.clear();
142 		QString fName = "";
143 		QString fDate = "";
144 		QString fType = "";
145 		if (name.contains("_emergency_"))
146 		{
147 			parts = name.split("_emergency_");
148 			fName = parts[0];
149 			fType = tr("Emergency");
150 			if (parts.count() > 1)
151 			{
152 				QStringList dateL = parts[1].split("_");
153 				if (dateL.count() > 4)
154 					fDate = dateL[0] + "." + dateL[1] + "." + dateL[2] + " " + dateL[3] + ":" + dateL[4];
155 			}
156 		}
157 		else if (name.contains("_autosave_"))
158 		{
159 			parts = name.split("_autosave_");
160 			fName = parts[0];
161 			fType = tr("Autosave");
162 			if (parts.count() > 1)
163 			{
164 				QStringList dateL = parts[1].split("_");
165 				if (dateL.count() > 4)
166 					fDate = dateL[0] + "." + dateL[1] + "." + dateL[2] + " " + dateL[3] + ":" + dateL[4];
167 			}
168 		}
169 		QCheckBox *cb = new QCheckBox(this);
170 		cb->setChecked(false);
171 		filesList->setCellWidget(a, 0, cb);
172 		connect(cb, SIGNAL(clicked()), this, SLOT(handleItemClick()));
173 		QTableWidgetItem *tW1 = new QTableWidgetItem(fName);
174 		tW1->setFlags(Qt::ItemIsEnabled);
175 		filesList->setItem(a, 1, tW1);
176 		QTableWidgetItem *tW2 = new QTableWidgetItem(fDate);
177 		tW2->setFlags(Qt::ItemIsEnabled);
178 		filesList->setItem(a, 2, tW2);
179 		QTableWidgetItem *tW3 = new QTableWidgetItem(fType);
180 		tW3->setFlags(Qt::ItemIsEnabled);
181 		filesList->setItem(a, 3, tW3);
182 	}
183 	filesList->setColumnWidth(0, 24);
184 	filesList->resizeColumnsToContents();
185 }
186