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 #include <QMessageBox>
9 
10 #include "commonstrings.h"
11 #include "prefs_shortwords.h"
12 #include "prefsstructs.h"
13 #include "swsyntaxhighlighter.h"
14 #include "version.h"
15 #include "scpaths.h"
16 #include "ui/scmessagebox.h"
17 
Prefs_ShortWords(QWidget * parent)18 Prefs_ShortWords::Prefs_ShortWords(QWidget* parent)
19 	: Prefs_Pane(parent)
20 {
21 	setupUi(this);
22 	languageChange();
23 
24 	m_caption = tr("Short Words");
25 	m_icon = "shortwords_16.png";
26 
27 	// defaults
28 	if (QFile::exists(RC_PATH_USR))
29 	{
30 		messageLabel->setText( tr("User settings"));
31 		loadCfgFile(RC_PATH_USR);
32 	}
33 	else
34 	{
35 		messageLabel->setText( tr("System wide configuration"));
36 		resetButton->setEnabled(false);
37 		loadCfgFile(RC_PATH);
38 	}
39 	saveButton->setEnabled(false);
40 	new SWSyntaxHighlighter(cfgEdit);
41 
42 	// signals
43 	connect(saveButton, SIGNAL(clicked()), this, SLOT(saveButton_pressed()));
44 	connect(resetButton, SIGNAL(clicked()), this, SLOT(resetButton_pressed()));
45 	connect(cfgEdit, SIGNAL(textChanged()), this, SLOT(cfgEdit_changed()));
46 }
47 
48 Prefs_ShortWords::~Prefs_ShortWords() = default;
49 
languageChange()50 void Prefs_ShortWords::languageChange()
51 {
52 }
53 
restoreDefaults(struct ApplicationPrefs * prefsData)54 void Prefs_ShortWords::restoreDefaults(struct ApplicationPrefs *prefsData)
55 {
56 }
57 
saveGuiToPrefs(struct ApplicationPrefs * prefsData) const58 void Prefs_ShortWords::saveGuiToPrefs(struct ApplicationPrefs *prefsData) const
59 {
60 }
61 
apply()62 void Prefs_ShortWords::apply()
63 {
64 	if (saveButton->isEnabled())
65 		saveButton_pressed();
66 }
67 
saveButton_pressed()68 void Prefs_ShortWords::saveButton_pressed()
69 {
70 	if (cfgEdit->document()->isModified() && QFile::exists(RC_PATH_USR))
71 	{
72 		if ((ScMessageBox::warning(this, tr("Short Words"),
73 				"<qt>" + tr("User configuration exists already. "
74 						"Do you really want to overwrite it?") + "</qt>",
75 				QMessageBox::Yes|QMessageBox::No,
76 				QMessageBox::NoButton,	// GUI default
77 				QMessageBox::Yes)	// batch default
78 			) == QMessageBox::No)
79 			return;
80 	}
81 
82 	QFile f(RC_PATH_USR);
83 	if (!f.open(QIODevice::WriteOnly))
84 	{
85 		ScMessageBox::warning(this, tr("Short Words"),
86 			 "<qt>" + tr("Cannot write file %1.").arg(RC_PATH_USR) + "</qt>");
87 	}
88 	QTextStream stream(&f);
89 	stream.setCodec("UTF-8");
90 	stream << cfgEdit->toPlainText();
91 	f.close();
92 	messageLabel->setText( tr("User settings saved"));
93 	saveButton->setEnabled(false);
94 }
95 
resetButton_pressed()96 void Prefs_ShortWords::resetButton_pressed()
97 {
98 	loadCfgFile(RC_PATH);
99 	QDir d;
100 	d.remove(RC_PATH_USR);
101 	saveButton->setEnabled(false);
102 	resetButton->setEnabled(false);
103 	messageLabel->setText( tr("System wide configuration reloaded"));
104 }
105 
cfgEdit_changed()106 void Prefs_ShortWords::cfgEdit_changed()
107 {
108 	resetButton->setEnabled(true);
109 	saveButton->setEnabled(true);
110 }
111 
loadCfgFile(const QString & filename)112 bool Prefs_ShortWords::loadCfgFile(const QString& filename)
113 {
114 	QFile f(filename);
115 	if (!f.open(QIODevice::ReadOnly))
116 	{
117 		messageLabel->setText( tr("Cannot open file %1").arg(f.fileName()));
118 		return false;
119 	}
120 	cfgEdit->clear();
121 	QTextStream stream(&f);
122 	stream.setCodec("UTF-8");
123 	while (!stream.atEnd())
124 		cfgEdit->append(stream.readLine());
125 	f.close();
126 	// it's a must to prevent "overwrite?" message box on saving prefs
127 	cfgEdit->document()->setModified(false);
128 	return true;
129 }
130 
131 
132