2 * Copyright (C) 2011 Toni Gundogdu <legatvs@gmail.com>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <QCoreApplication>
21 #include <QFileDialog>
22 #include <QGridLayout>
23 #include <QVBoxLayout>
24 #include <QHBoxLayout>
30 #include <NSettingsMutator>
31 #include <NSettingsDialog>
32 #include <NTripwireEdit>
34 extern nn::NSettingsMutator settings
; // main.cpp
39 NSettingsDownload::NSettingsDownload(QWidget
*parent
/*=NULL*/)
40 : NSettingsWidget(parent
),
51 QGroupBox
*fngBox
= new QGroupBox(tr("Save downloaded media as"));
53 QGridLayout
*g
= new QGridLayout
;
55 addGridRow(g
, tr("&Filename format:"),
56 &fnfmtCombo
, &fnfmtEdit
, SLOT(fnfmtChanged(int)));
58 addGridRow(g
, tr("&Regular expression:"),
59 ®expCombo
, ®expEdit
, SLOT(regexpChanged(int)));
61 addGridRow(g
, tr("Example:"), NULL
, &exampleEdit
, NULL
);
65 fnfmtCombo
->addItem(tr("Default"), "%t.%s");
66 fnfmtCombo
->addItem(tr("With media ID"), "%t_%i.%s");
67 fnfmtCombo
->addItem(tr("With ID, domain"), "%t_%i_%d.%s");
69 regexpCombo
->addItem(tr("Default"), "/(\\w|\\s)/g");
70 regexpCombo
->addItem(tr("Word characters"), "/(\\w)/g");
71 regexpCombo
->addItem(tr("Digit characters"), "/(\\d)/g");
73 exampleEdit
->setReadOnly(true);
77 savedirEdit
= new NTripwireEdit
;
78 savedirEdit
->setReadOnly(true);
80 connect(savedirEdit
, SIGNAL(entered()), this, SLOT(browseDir()));
82 QHBoxLayout
*dirBox
= new QHBoxLayout
;
83 dirBox
->addWidget(savedirEdit
);
84 dirBox
->addStretch(0);
86 QGroupBox
*dirgBox
= new QGroupBox(tr("&Save downloaded media in"));
87 dirgBox
->setLayout(dirBox
);
91 QVBoxLayout
*box
= new QVBoxLayout
;
92 box
->addWidget(fngBox
);
93 box
->addWidget(dirgBox
);
98 void NSettingsDownload::init()
100 if (fnfmtEdit
->text().isEmpty())
103 if (regexpEdit
->text().isEmpty())
108 if (savedirEdit
->text().isEmpty())
109 savedirEdit
->setText(QDir::homePath());
112 static bool _verify(QLineEdit
*edit
, QString
& msg
)
114 if (edit
->text().isEmpty())
116 msg
= qApp
->translate("nn::NSettingsDownload",
117 "Please fill the required fields");
119 return msg
.isEmpty();
122 bool NSettingsDownload::verify(QString
& msg
)
124 bool r
= _verify(fnfmtEdit
, msg
);
126 r
= _verify(regexpEdit
, msg
);
130 static void _write(SettingKey k
, QLineEdit
*e
)
132 settings
.setValue(k
, e
->text());
135 void NSettingsDownload::write()
138 _write(FilenameFormat
, fnfmtEdit
);
139 _write(FilenameRegExp
, regexpEdit
);
141 _write(SaveMediaDirectory
, savedirEdit
);
144 static void _read(SettingKey key
, QLineEdit
*edit
, QComboBox
*combo
)
146 const QString text
= settings
.value(key
).toString();
150 const int n
= combo
->findData(text
);
152 combo
->setCurrentIndex(n
);
158 void NSettingsDownload::read()
161 _read(FilenameFormat
, fnfmtEdit
, fnfmtCombo
);
162 _read(FilenameRegExp
, regexpEdit
, regexpCombo
);
164 _read(SaveMediaDirectory
, savedirEdit
, NULL
);
167 void NSettingsDownload::addGridRow(
174 QLabel
*l
= new QLabel(text
);
183 connect(*c
, SIGNAL(currentIndexChanged(int)),
187 connect(*e
, SIGNAL(editingFinished()),
188 this, SLOT(updateExample()));
190 const int row
= g
->rowCount() + 1;
192 g
->addWidget(l
, row
, 0);
193 g
->addWidget(*e
, row
, 1);
195 g
->addWidget(*c
, row
, 2);
198 void NSettingsDownload::fnfmtChanged(int n
)
200 fnfmtEdit
->setText(fnfmtCombo
->itemData(n
).toString());
204 void NSettingsDownload::regexpChanged(int n
)
206 regexpEdit
->setText(regexpCombo
->itemData(n
).toString());
210 static const QString e_title
= "Foo bar";
211 static const QString e_domain
= "baz";
212 static const QString e_media_id
= "abcd1234";
213 static const QString e_suffix
= "flv";
215 void NSettingsDownload::updateExample()
217 if (!fnfmtEdit
|| !regexpEdit
|| !exampleEdit
)
220 QString fname
= fnfmtEdit
->text();
221 const bool r
= format_filename(regexpEdit
->text(),
230 exampleEdit
->setText(fname
);
233 void NSettingsDownload::browseDir()
235 const QString dir
= QFileDialog::getExistingDirectory(
237 tr("Open directory"),
239 QFileDialog::ShowDirsOnly
240 | QFileDialog::DontResolveSymlinks
);
242 savedirEdit
->setText(dir
);
247 /* vim: set ts=2 sw=2 tw=72 expandtab: */