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) 2004 by Riku Leino                                      *
9  *   tsoots@gmail.com                                                      *
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 "commonstrings.h"
28 #include "gtdialogs.h"
29 #include "iconmanager.h"
30 #include "prefscontext.h"
31 #include "prefsfile.h"
32 #include "prefsmanager.h"
33 #include "ui/gtfiledialog.h"
34 
35 #include <QFileInfo>
36 #include <QHBoxLayout>
37 #include <QLabel>
38 #include <QPixmap>
39 #include <QPushButton>
40 #include <QToolTip>
41 #include <QVBoxLayout>
42 
43 extern QString DocDir;
44 
45 /********* Class gtImporterDialog*******************************************************************/
46 
gtImporterDialog(const QString & fileName,const QStringList & importers,int currentSelection)47 gtImporterDialog::gtImporterDialog(const QString& fileName, const QStringList& importers, int currentSelection)
48 {
49 	QFileInfo fInfo(fileName);
50 	QString ext = fInfo.suffix();
51 	if ((ext.length() > 0) && !ext.startsWith("."))
52 		ext.prepend(".");
53 
54 	setWindowTitle( tr("Choose the importer to use"));
55 	setWindowIcon(IconManager::instance().loadIcon("AppIcon.png"));
56 
57 	QBoxLayout* layout = new QVBoxLayout(this);
58 
59 	QBoxLayout* llayout = new QHBoxLayout;
60 	llayout->setContentsMargins(0, 0, 0, 0);
61 	llayout->setSpacing(6);
62 
63 	QString labelText;
64 	if (ext.length() > 0)
65 		labelText = tr("Choose the importer to use for %1 file:").arg(ext);
66 	else
67 		labelText = tr("Choose the importer to use:");
68 	QLabel* label = new QLabel(labelText, this);
69 	llayout->addWidget(label);
70 	layout->addLayout(llayout);
71 
72 	QBoxLayout* ilayout = new QHBoxLayout;
73 	ilayout->setContentsMargins(0, 0, 0, 0);
74 	ilayout->setSpacing(6);
75 	importerCombo = new QComboBox(this);
76 	importerCombo->setMinimumSize(QSize(150, 0));
77 	importerCombo->setToolTip( tr("Choose the importer to use"));
78 	importerCombo->addItems(importers);
79 	if (static_cast<int>(importers.count()) > currentSelection)
80 		importerCombo->setCurrentIndex(currentSelection);
81 	else
82 		importerCombo->setCurrentIndex(0);
83 	ilayout->addWidget(importerCombo);
84 	layout->addLayout(ilayout);
85 
86 	QBoxLayout* dlayout = new QHBoxLayout;
87 	dlayout->setContentsMargins(0, 0, 0, 0);
88 	dlayout->setSpacing(6);
89 	rememberCheck = new QCheckBox( tr("Remember association"), this);
90 	rememberCheck->setChecked(false);
91 	rememberCheck->setToolTip( "<qt>" + tr("Remember the file extension - importer association and do not ask again to select an importer for files of this type.") + "</qt>" );
92 	dlayout->addStretch(10);
93 	dlayout->addWidget(rememberCheck);
94 	layout->addLayout(dlayout);
95 
96 	QBoxLayout* blayout = new QHBoxLayout;
97 	blayout->setContentsMargins(0, 0, 0, 0);
98 	blayout->setSpacing(6);
99 	blayout->addStretch(10);
100 	okButton = new QPushButton( CommonStrings::tr_OK, this);
101 	blayout->addWidget(okButton);
102 	layout->addLayout(blayout);
103 
104 	connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
105 }
106 
shouldRemember()107 bool gtImporterDialog::shouldRemember()
108 {
109 	return rememberCheck->isChecked();
110 }
111 
getImporter()112 QString gtImporterDialog::getImporter()
113 {
114 	return importerCombo->currentText();
115 }
116 
~gtImporterDialog()117 gtImporterDialog::~gtImporterDialog()
118 {
119 
120 }
121 
122 /********* Class gtDialogs *************************************************************************/
123 
gtDialogs()124 gtDialogs::gtDialogs()
125 {
126 	m_fdia = nullptr;
127 	m_fileName = "";
128 	m_encoding = "";
129 	m_importer = -1;
130 	m_prefs = PrefsManager::instance().prefsFile->getContext("gtDialogs");
131 }
132 
runFileDialog(const QString & filters,const QStringList & importers)133 bool gtDialogs::runFileDialog(const QString& filters, const QStringList& importers)
134 {
135 	bool accepted = false;
136 	PrefsContext* dirs = PrefsManager::instance().prefsFile->getContext("dirs");
137 	QString dir = dirs->get("get_text", ".");
138 
139 	m_fdia = new gtFileDialog(filters, importers, dir);
140 	if (m_fdia->exec() == QDialog::Accepted)
141 	{
142 		m_fileName = m_fdia->selectedFile();
143 		if (!m_fileName.isEmpty())
144 			accepted = true;
145 		m_encoding = m_fdia->encodingCombo->currentText();
146 		m_importer = m_fdia->importerCombo->currentIndex() - 1;
147 		dirs->set("get_text", m_fileName.left(m_fileName.lastIndexOf("/")));
148 	}
149 	return accepted;
150 }
151 
runImporterDialog(const QString & fileName,const QStringList & importers)152 bool gtDialogs::runImporterDialog(const QString& fileName, const QStringList& importers)
153 {
154 	bool ok = false;
155 	bool shouldRemember = false;
156 
157 	QFileInfo fileInfo(fileName);
158 	QString fileExtension = fileInfo.suffix();
159 	if (fileExtension.length() > 0 && !fileExtension.startsWith("."))
160 		fileExtension.prepend(".");
161 	if (fileExtension.isEmpty())
162 		fileExtension = ".no_extension";
163 
164 	QString imp = m_prefs->get("remember"+ fileExtension, QString("false"));
165 	QString res = "";
166 	if (imp != "false")
167 	{
168 		res = imp;
169 		if (importers.contains(res))
170 			ok = true;
171 	}
172 
173 	if (!ok)
174 	{
175 		int curSel = m_prefs->getInt("curSel", 0);
176 		int extensionSel = m_prefs->getInt(fileExtension, -1);
177 		if ((extensionSel > -1) && (extensionSel < static_cast<int>(importers.count())))
178 			curSel = extensionSel;
179 		else
180 			curSel = 0;
181 		gtImporterDialog* idia = new gtImporterDialog(fileName, importers, curSel);
182 		if (idia->exec())
183 		{
184 			res = idia->getImporter();
185 			shouldRemember = idia->shouldRemember();
186 			ok = true;
187 		}
188 		delete idia;
189 	}
190 
191 	if (ok)
192 	{
193 		QString fileExtension;
194 		int importerIndex = importers.indexOf(res);
195 		if (importerIndex >= 0)
196 		{
197 			m_importer = importerIndex;
198 			m_prefs->set("curSel", static_cast<int>(importerIndex));
199 			if (!fileExtension.isEmpty())
200 			{
201 				m_prefs->set(fileExtension, static_cast<int>(importerIndex));
202 				if (shouldRemember)
203 					m_prefs->set("remember" + fileExtension, res);
204 			}
205 		}
206 	}
207 	return ok;
208 }
209 
getFileName()210 const QString& gtDialogs::getFileName()
211 {
212 	return m_fileName;
213 }
214 
getEncoding()215 const QString& gtDialogs::getEncoding()
216 {
217 	return m_encoding;
218 }
219 
getImporter()220 int gtDialogs::getImporter()
221 {
222 	return m_importer;
223 }
224 
importTextOnly()225 bool gtDialogs::importTextOnly()
226 {
227 	bool ret = false;
228 	if (m_fdia)
229 		ret = m_fdia->textOnlyCheckBox->isChecked();
230 	return ret;
231 }
232 
prefixStyles()233 bool gtDialogs::prefixStyles()
234 {
235 	bool ret = false;
236 	if (m_fdia)
237 		ret = m_fdia->prefixStylesCheckBox->isChecked();
238 	return ret;
239 }
240 
~gtDialogs()241 gtDialogs::~gtDialogs()
242 {
243 	delete m_fdia;
244 }
245