Version 2.6
[qgit4/redivivus.git] / src / settingsimpl.cpp
blob27c5e9fafb62ab5cfad5c4d245b14e5f3f68c4e1
1 /*
2 Description: settings dialog
4 Author: Marco Costalba (C) 2005-2007
6 Copyright: See COPYING file that comes with this distribution
8 */
9 #include <QTextCodec>
10 #include <QFileDialog>
11 #include <QFontDialog>
12 #include <QSettings>
13 #include "common.h"
14 #include "git.h"
15 #include "settingsimpl.h"
18 By default, there are two entries in the search path:
20 1. SYSCONF - where SYSCONF is a directory specified when configuring Qt;
21 by default it is INSTALL/etc/settings.
22 2. $HOME/.qt/ - where $HOME is the user's home directory.
25 static const char* en[] = { "Latin1", "Big5 -- Chinese", "EUC-JP -- Japanese",
26 "EUC-KR -- Korean", "GB18030 -- Chinese", "ISO-2022-JP -- Japanese",
27 "Shift_JIS -- Japanese", "UTF-8 -- Unicode, 8-bit",
28 "KOI8-R -- Russian", "KOI8-U -- Ukrainian", "ISO-8859-1 -- Western",
29 "ISO-8859-2 -- Central European", "ISO-8859-3 -- Central European",
30 "ISO-8859-4 -- Baltic", "ISO-8859-5 -- Cyrillic", "ISO-8859-6 -- Arabic",
31 "ISO-8859-7 -- Greek", "ISO-8859-8 -- Hebrew, visually ordered",
32 "ISO-8859-8-i -- Hebrew, logically ordered", "ISO-8859-9 -- Turkish",
33 "ISO-8859-10", "ISO-8859-13", "ISO-8859-14", "ISO-8859-15 -- Western",
34 "windows-1250 -- Central European", "windows-1251 -- Cyrillic",
35 "windows-1252 -- Western", "windows-1253 -- Greek", "windows-1254 -- Turkish",
36 "windows-1255 -- Hebrew", "windows-1256 -- Arabic", "windows-1257 -- Baltic",
37 "windows-1258", 0 };
39 using namespace QGit;
41 SettingsImpl::SettingsImpl(QWidget* p, Git* g, int defTab) : QDialog(p), git(g) {
43 setupUi(this);
44 int f = flags(FLAGS_KEY);
45 checkBoxDiffCache->setChecked(f & DIFF_INDEX_F);
46 checkBoxNumbers->setChecked(f & NUMBERS_F);
47 checkBoxSign->setChecked(f & SIGN_PATCH_F);
48 checkBoxCommitSign->setChecked(f & SIGN_CMT_F);
49 checkBoxCommitVerify->setChecked(f & VERIFY_CMT_F);
50 checkBoxCommitUseDefMsg->setChecked(f & USE_CMT_MSG_F);
51 checkBoxRangeSelectDialog->setChecked(f & RANGE_SELECT_F);
52 checkBoxReopenLastRepo->setChecked(f & REOPEN_REPO_F);
53 checkBoxRelativeDate->setChecked(f & REL_DATE_F);
54 checkBoxLogDiffTab->setChecked(f & LOG_DIFF_TAB_F);
55 checkBoxSmartLabels->setChecked(f & SMART_LBL_F);
56 checkBoxMsgOnNewSHA->setChecked(f & MSG_ON_NEW_F);
58 QSettings set;
59 SCRef APOpt(set.value(AM_P_OPT_KEY).toString());
60 SCRef FPOpt(set.value(FMT_P_OPT_KEY).toString());
61 SCRef extDiff(set.value(EXT_DIFF_KEY, EXT_DIFF_DEF).toString());
62 SCRef exFile(set.value(EX_KEY, EX_DEF).toString());
63 SCRef exPDir(set.value(EX_PER_DIR_KEY, EX_PER_DIR_DEF).toString());
64 SCRef tmplt(set.value(CMT_TEMPL_KEY, CMT_TEMPL_DEF).toString());
65 SCRef CMArgs(set.value(CMT_ARGS_KEY).toString());
67 lineEditApplyPatchExtraOptions->setText(APOpt);
68 lineEditFormatPatchExtraOptions->setText(FPOpt);
69 lineEditExternalDiffViewer->setText(extDiff);
70 lineEditExcludeFile->setText(exFile);
71 lineEditExcludePerDir->setText(exPDir);
72 lineEditTemplate->setText(tmplt);
73 lineEditCommitExtraOptions->setText(CMArgs);
74 lineEditTypeWriterFont->setText(TYPE_WRITER_FONT.toString());
75 lineEditTypeWriterFont->setCursorPosition(0); // font description could be long
77 setupCodecsCombo();
78 checkBoxDiffCache_toggled(checkBoxDiffCache->isChecked());
79 tabDialog->setCurrentIndex(defTab);
80 userInfo();
81 comboBoxGitConfigSource_activated(0);
84 void SettingsImpl::userInfo() {
86 QGit::userInfo() returns a QStringList formed by
87 triples (defined in, user, email)
89 git->userInfo(_uInfo);
90 if (_uInfo.count() % 3 != 0) {
91 dbs("ASSERT in SettingsImpl::userInfo(), bad info returned");
92 return;
94 bool found = false;
95 int idx = 0;
96 FOREACH_SL(it, _uInfo) {
97 comboBoxUserSrc->addItem(*it);
98 ++it;
99 if (!found && !(*it).isEmpty())
100 found = true;
101 if (!found)
102 idx++;
103 ++it;
105 if (!found)
106 idx = 0;
108 comboBoxUserSrc->setCurrentIndex(idx);
109 comboBoxUserSrc_activated(idx);
112 void SettingsImpl::addConfigOption(QTreeWidgetItem* parent, QStringList paths, const QString& value) {
114 if (paths.isEmpty()) {
115 parent->setText(1, value);
116 return;
118 QString name(paths.first());
119 paths.removeFirst();
121 // Options list is already ordered
122 if (parent->childCount() == 0 || name != parent->child(0)->text(0))
123 parent->addChild(new QTreeWidgetItem(parent, QStringList(name)));
125 addConfigOption(parent->child(parent->childCount() - 1), paths, value);
128 void SettingsImpl::readGitConfig(const QString& source) {
130 populatingGitConfig = true;
131 treeWidgetGitConfig->clear();
132 QStringList options(git->getGitConfigList(source == "Global"));
133 options.sort();
135 FOREACH_SL(it, options) {
137 QStringList paths = it->split("=").at(0).split(".");
138 QString value = it->split("=").at(1);
140 if (paths.isEmpty() || value.isEmpty()) {
141 dbp("SettingsImpl::readGitConfig Unable to parse line %1", *it);
142 continue;
144 QString name(paths.first());
145 paths.removeFirst();
146 QList<QTreeWidgetItem*> items = treeWidgetGitConfig->findItems(name, Qt::MatchExactly);
147 QTreeWidgetItem* item;
149 if (items.isEmpty())
150 item = new QTreeWidgetItem(treeWidgetGitConfig, QStringList(name));
151 else
152 item = items.first();
154 addConfigOption(item, paths, value);
156 populatingGitConfig = false;
159 void SettingsImpl::treeWidgetGitConfig_itemChanged(QTreeWidgetItem* item, int i) {
161 if (populatingGitConfig)
162 return;
163 dbs(item->text(0));dbs(item->text(1));dbp("column %1", i);
166 void SettingsImpl::comboBoxUserSrc_activated(int i) {
168 lineEditAuthor->setText(_uInfo[i * 3 + 1]);
169 lineEditMail->setText(_uInfo[i * 3 + 2]);
172 void SettingsImpl::comboBoxGitConfigSource_activated(int) {
174 readGitConfig(comboBoxGitConfigSource->currentText());
177 void SettingsImpl::writeSetting(const QString& key, const QVariant& value) {
179 QSettings settings;
180 settings.setValue(key, value);
183 void SettingsImpl::setupCodecList(QStringList& list) {
185 int i = 0;
186 while (en[i] != 0)
187 list.append(QString::fromLatin1(en[i++]));
190 void SettingsImpl::setupCodecsCombo() {
192 const QString localCodec(QTextCodec::codecForLocale()->name());
193 QStringList codecs;
194 codecs.append(QString("Local Codec (" + localCodec + ")"));
195 setupCodecList(codecs);
196 comboBoxCodecs->insertItems(0, codecs);
198 bool isGitArchive;
199 QTextCodec* tc = git->getTextCodec(&isGitArchive);
200 if (!isGitArchive) {
201 comboBoxCodecs->setEnabled(false);
202 return;
204 const QString curCodec(tc != 0 ? tc->name() : "Latin1");
205 QRegExp re("*" + curCodec + "*", Qt::CaseInsensitive, QRegExp::Wildcard);
206 int idx = codecs.indexOf(re);
207 if (idx == -1) {
208 dbp("ASSERT: codec <%1> not available, using local codec", curCodec);
209 idx = 0;
211 comboBoxCodecs->setCurrentIndex(idx);
212 if (idx == 0) // signal activated() will not fire in this case
213 comboBoxCodecs_activated(0);
216 void SettingsImpl::comboBoxCodecs_activated(int idx) {
218 QString codecName(QTextCodec::codecForLocale()->name());
219 if (idx != 0)
220 codecName = comboBoxCodecs->currentText().section(" --", 0, 0);
222 git->setTextCodec(QTextCodec::codecForName(codecName.toLatin1()));
225 void SettingsImpl::pushButtonExtDiff_clicked() {
227 QString extDiffName(QFileDialog::getOpenFileName(this,
228 "Select the patch viewer"));
229 if (!extDiffName.isEmpty())
230 lineEditExternalDiffViewer->setText(extDiffName);
233 void SettingsImpl::pushButtonFont_clicked() {
235 bool ok;
236 QFont fnt = QFontDialog::getFont(&ok, TYPE_WRITER_FONT, this);
237 if (ok && TYPE_WRITER_FONT != fnt) {
239 TYPE_WRITER_FONT = fnt;
240 lineEditTypeWriterFont->setText(fnt.toString());
241 lineEditTypeWriterFont->setCursorPosition(0);
242 writeSetting(TYPWRT_FNT_KEY, fnt.toString());
244 emit typeWriterFontChanged();
248 void SettingsImpl::changeFlag(uint f, bool b) {
250 setFlag(f, b);
251 emit flagChanged(f);
254 void SettingsImpl::checkBoxDiffCache_toggled(bool b) {
256 lineEditExcludeFile->setEnabled(b);
257 lineEditExcludePerDir->setEnabled(b);
258 changeFlag(DIFF_INDEX_F, b);
261 void SettingsImpl::checkBoxNumbers_toggled(bool b) {
263 changeFlag(NUMBERS_F, b);
266 void SettingsImpl::checkBoxSign_toggled(bool b) {
268 changeFlag(SIGN_PATCH_F, b);
271 void SettingsImpl::checkBoxRangeSelectDialog_toggled(bool b) {
273 changeFlag(RANGE_SELECT_F, b);
276 void SettingsImpl::checkBoxReopenLastRepo_toggled(bool b) {
278 changeFlag(REOPEN_REPO_F, b);
281 void SettingsImpl::checkBoxRelativeDate_toggled(bool b) {
283 changeFlag(REL_DATE_F, b);
286 void SettingsImpl::checkBoxLogDiffTab_toggled(bool b) {
288 changeFlag(LOG_DIFF_TAB_F, b);
291 void SettingsImpl::checkBoxSmartLabels_toggled(bool b) {
293 changeFlag(SMART_LBL_F, b);
296 void SettingsImpl::checkBoxMsgOnNewSHA_toggled(bool b) {
298 changeFlag(MSG_ON_NEW_F, b);
301 void SettingsImpl::checkBoxCommitSign_toggled(bool b) {
303 changeFlag(SIGN_CMT_F, b);
306 void SettingsImpl::checkBoxCommitVerify_toggled(bool b) {
308 changeFlag(VERIFY_CMT_F, b);
311 void SettingsImpl::checkBoxCommitUseDefMsg_toggled(bool b) {
313 changeFlag(USE_CMT_MSG_F, b);
316 void SettingsImpl::lineEditExternalDiffViewer_textChanged(const QString& s) {
318 writeSetting(EXT_DIFF_KEY, s);
321 void SettingsImpl::lineEditApplyPatchExtraOptions_textChanged(const QString& s) {
323 writeSetting(AM_P_OPT_KEY, s);
326 void SettingsImpl::lineEditFormatPatchExtraOptions_textChanged(const QString& s) {
328 writeSetting(FMT_P_OPT_KEY, s);
331 void SettingsImpl::lineEditExcludeFile_textChanged(const QString& s) {
333 writeSetting(EX_KEY, s);
336 void SettingsImpl::lineEditExcludePerDir_textChanged(const QString& s) {
338 writeSetting(EX_PER_DIR_KEY, s);
341 void SettingsImpl::lineEditTemplate_textChanged(const QString& s) {
343 writeSetting(CMT_TEMPL_KEY, s);
346 void SettingsImpl::lineEditCommitExtraOptions_textChanged(const QString& s) {
348 writeSetting(CMT_ARGS_KEY, s);