00001 #include "recordingprofile.h"
00002 #include "videosource.h"
00003 #include "profilegroup.h"
00004 #include "libmyth/mythcontext.h"
00005 #include "libmyth/mythdbcon.h"
00006 #include <qsqldatabase.h>
00007 #include <qheader.h>
00008 #include <qcursor.h>
00009 #include <qlayout.h>
00010 #include <iostream>
00011 #include <qaccel.h>
00012
00013 QString ProfileGroupStorage::whereClause(MSqlBindings &bindings)
00014 {
00015 QString idTag(":WHEREID");
00016 QString query("id = " + idTag);
00017
00018 bindings.insert(idTag, parent.getProfileNum());
00019
00020 return query;
00021 }
00022
00023 QString ProfileGroupStorage::setClause(MSqlBindings &bindings)
00024 {
00025 QString idTag(":SETID");
00026 QString colTag(":SET" + getColumn().upper());
00027
00028 QString query("id = " + idTag + ", " +
00029 getColumn() + " = " + colTag);
00030
00031 bindings.insert(idTag, parent.getProfileNum());
00032 bindings.insert(colTag, setting->getValue().utf8());
00033
00034 return query;
00035 }
00036
00037 void ProfileGroup::HostName::fillSelections()
00038 {
00039 QStringList hostnames;
00040 ProfileGroup::getHostNames(&hostnames);
00041 for(QStringList::Iterator it = hostnames.begin();
00042 it != hostnames.end(); it++)
00043 this->addSelection(*it);
00044 }
00045
00046 ProfileGroup::ProfileGroup()
00047 {
00048
00049 addChild(id = new ID());
00050 addChild(is_default = new Is_default(*this));
00051
00052 ConfigurationGroup* profile = new VerticalConfigurationGroup(false);
00053 profile->setLabel(QObject::tr("ProfileGroup"));
00054 profile->addChild(name = new Name(*this));
00055 CardInfo *cardInfo = new CardInfo(*this);
00056 profile->addChild(cardInfo);
00057 CardType::fillSelections(cardInfo);
00058 host = new HostName(*this);
00059 profile->addChild(host);
00060 host->fillSelections();
00061 addChild(profile);
00062 };
00063
00064 void ProfileGroup::loadByID(int profileId) {
00065 id->setValue(profileId);
00066 load();
00067 }
00068
00069 void ProfileGroup::fillSelections(SelectSetting* setting) {
00070 QStringList cardtypes;
00071 QString transcodeID;
00072
00073 MSqlQuery result(MSqlQuery::InitCon());
00074
00075 result.prepare("SELECT DISTINCT cardtype FROM capturecard;");
00076
00077 if (result.exec() && result.isActive() && result.size() > 0)
00078 {
00079 while (result.next())
00080 {
00081 cardtypes.append(result.value(0).toString());
00082 }
00083 }
00084
00085 result.prepare("SELECT name,id,hostname,is_default,cardtype "
00086 "FROM profilegroups;");
00087
00088 if (result.exec() && result.isActive() && result.size() > 0)
00089 while (result.next())
00090 {
00091
00092 if (result.value(3).toInt())
00093 {
00094 bool match = false;
00095 for(QStringList::Iterator it = cardtypes.begin();
00096 it != cardtypes.end(); it++)
00097 if (result.value(4).toString() == *it)
00098 match = true;
00099
00100 if (! match)
00101 {
00102 if (result.value(4).toString() == "TRANSCODE")
00103 transcodeID = result.value(1).toString();
00104 continue;
00105 }
00106 }
00107 QString value = QString::fromUtf8(result.value(0).toString());
00108 if (result.value(2).toString() != NULL &&
00109 result.value(2).toString() != "")
00110 value += QString(" (%1)").arg(result.value(2).toString());
00111 setting->addSelection(value, result.value(1).toString());
00112 }
00113 if (! transcodeID.isNull())
00114 setting->addSelection(QObject::tr("Transcoders"), transcodeID);
00115 }
00116
00117 QString ProfileGroup::getName(int group)
00118 {
00119 MSqlQuery result(MSqlQuery::InitCon());
00120 QString querystr = QString("SELECT name from profilegroups WHERE id = %1")
00121 .arg(group);
00122 result.prepare(querystr);
00123
00124 if (result.exec() && result.isActive() && result.size() > 0)
00125 {
00126 result.next();
00127 return QString::fromUtf8(result.value(0).toString());
00128 }
00129
00130 return NULL;
00131 }
00132
00133 bool ProfileGroup::allowedGroupName(void)
00134 {
00135 MSqlQuery result(MSqlQuery::InitCon());
00136 QString querystr = QString("SELECT DISTINCT id FROM profilegroups WHERE "
00137 "name = '%1' AND hostname = '%2';")
00138 .arg(getName()).arg(host->getValue());
00139 result.prepare(querystr);
00140
00141 if (result.exec() && result.isActive() && result.size() > 0)
00142 return false;
00143 return true;
00144 }
00145
00146 void ProfileGroup::getHostNames(QStringList *hostnames)
00147 {
00148 hostnames->clear();
00149
00150 MSqlQuery result(MSqlQuery::InitCon());
00151
00152 result.prepare("SELECT DISTINCT hostname from capturecard");
00153
00154 if (result.exec() && result.isActive() && result.size() > 0)
00155 {
00156 while (result.next())
00157 hostnames->append(result.value(0).toString());
00158 }
00159 }
00160
00161 void ProfileGroupEditor::open(int id) {
00162
00163 ProfileGroup* profilegroup = new ProfileGroup();
00164
00165 bool isdefault = false;
00166 bool show_profiles = true;
00167 bool newgroup = false;
00168 int profileID;
00169 QString pgName;
00170
00171 if (id != 0)
00172 {
00173 profilegroup->loadByID(id);
00174 pgName = profilegroup->getName();
00175 if (profilegroup->isDefault())
00176 isdefault = true;
00177 }
00178 else
00179 {
00180 pgName = QString(QObject::tr("New Profile Group Name"));
00181 profilegroup->setName(pgName);
00182 newgroup = true;
00183 }
00184
00185 if (! isdefault)
00186 {
00187 if (profilegroup->exec(false) == QDialog::Accepted &&
00188 profilegroup->allowedGroupName())
00189 {
00190 profilegroup->save();
00191 profileID = profilegroup->getProfileNum();
00192 QValueList <int> found;
00193
00194 MSqlQuery result(MSqlQuery::InitCon());
00195 QString querystr = QString("SELECT name FROM recordingprofiles WHERE "
00196 "profilegroup = %1").arg(profileID);
00197 result.prepare(querystr);
00198
00199 if (result.exec() && result.isActive() && result.size() > 0)
00200 {
00201 while (result.next())
00202 {
00203 for (int i = 0; availProfiles[i] != ""; i++)
00204 if (result.value(0).toString() == availProfiles[i])
00205 found.push_back(i);
00206 }
00207 }
00208
00209 for(int i = 0; availProfiles[i] != ""; i++)
00210 {
00211 bool skip = false;
00212 for (QValueList <int>::Iterator j = found.begin();
00213 j != found.end(); j++)
00214 if (i == *j)
00215 skip = true;
00216 if (! skip)
00217 {
00218 result.prepare("INSERT INTO recordingprofiles "
00219 "(name, profilegroup) VALUES (:NAME, :PROFID);");
00220 result.bindValue(":NAME", availProfiles[i]);
00221 result.bindValue(":PROFID", profileID);
00222 result.exec();
00223 }
00224 }
00225 }
00226 else if (newgroup)
00227 show_profiles = false;
00228 }
00229
00230 if (show_profiles)
00231 {
00232 pgName = profilegroup->getName();
00233 profileID = profilegroup->getProfileNum();
00234 RecordingProfileEditor editor(profileID, pgName);
00235 editor.exec();
00236 }
00237 delete profilegroup;
00238 };
00239
00240 void ProfileGroupEditor::load(void)
00241 {
00242 listbox->clearSelections();
00243 ProfileGroup::fillSelections(listbox);
00244 listbox->addSelection(QObject::tr("(Create new profile group)"), "0");
00245 }
00246
00247 DialogCode ProfileGroupEditor::exec(void)
00248 {
00249 DialogCode ret = kDialogCodeAccepted;
00250 redraw = true;
00251
00252 while ((QDialog::Accepted == ret) || redraw)
00253 {
00254 redraw = false;
00255
00256 load();
00257
00258 dialog = new ConfigurationDialogWidget(gContext->GetMainWindow(),
00259 "ProfileGroupEditor");
00260
00261 connect(dialog, SIGNAL(menuButtonPressed()), this, SLOT(callDelete()));
00262
00263 int width = 0, height = 0;
00264 float wmult = 0.0f, hmult = 0.0f;
00265 gContext->GetScreenSettings(width, wmult, height, hmult);
00266
00267 QVBoxLayout *layout = new QVBoxLayout(dialog, (int)(20 * hmult));
00268 layout->addWidget(listbox->configWidget(NULL, dialog));
00269
00270 dialog->Show();
00271
00272 ret = dialog->exec();
00273
00274 dialog->deleteLater();
00275 dialog = NULL;
00276
00277 if (ret == QDialog::Accepted)
00278 open(listbox->getValue().toInt());
00279 }
00280
00281 return kDialogCodeRejected;
00282 }
00283
00284 void ProfileGroupEditor::callDelete(void)
00285 {
00286 int id = listbox->getValue().toInt();
00287
00288 MSqlQuery result(MSqlQuery::InitCon());
00289 QString querystr = QString("SELECT id FROM profilegroups WHERE "
00290 "id = %1 AND is_default = 0;").arg(id);
00291 result.prepare(querystr);
00292
00293 if (result.exec() && result.isActive() && result.size() > 0)
00294 {
00295 result.next();
00296 QString message = QObject::tr("Delete profile group:") +
00297 QString("\n'%1'?").arg(ProfileGroup::getName(id));
00298
00299 DialogCode value = MythPopupBox::Show2ButtonPopup(
00300 gContext->GetMainWindow(),
00301 "", message,
00302 QObject::tr("Yes, delete group"),
00303 QObject::tr("No, Don't delete group"), kDialogCodeButton1);
00304
00305 if (kDialogCodeButton0 == value)
00306 {
00307 querystr = QString("DELETE codecparams FROM codecparams, "
00308 "recordingprofiles WHERE "
00309 "codecparams.profile = recordingprofiles.id "
00310 "AND recordingprofiles.profilegroup = %1").arg(id);
00311 result.prepare(querystr);
00312 result.exec();
00313
00314 querystr = QString("DELETE FROM recordingprofiles WHERE "
00315 "profilegroup = %1").arg(id);
00316 result.prepare(querystr);
00317 result.exec();
00318
00319 querystr = QString("DELETE FROM profilegroups WHERE id = %1;").arg(id);
00320 result.prepare(querystr);
00321 result.exec();
00322
00323 redraw = true;
00324
00325 if (dialog)
00326 dialog->done(QDialog::Rejected);
00327 }
00328 }
00329
00330 }
00331