00001
00002 #include <iostream>
00003
00004
00005 #include "mythlogging.h"
00006 #include "mythdb.h"
00007 #include "channelutil.h"
00008 #include "channelgroup.h"
00009 #include "channelgroupsettings.h"
00010
00011 #define LOC QString("Channel Group Settings: ")
00012
00013
00014 class ChannelGroupStorage : public Storage
00015 {
00016 public:
00017 ChannelGroupStorage(Setting *_setting,
00018 uint _chanid, QString _grpname) :
00019 setting(_setting), chanid(_chanid), grpname(_grpname), grpid(0) {}
00020 virtual ~ChannelGroupStorage() {};
00021
00022 virtual void Load(void);
00023 virtual void Save(void);
00024 virtual void Save(QString destination);
00025
00026 protected:
00027 Setting *setting;
00028 uint chanid;
00029 QString grpname;
00030 int grpid;
00031 };
00032
00033 void ChannelGroupStorage::Load(void)
00034 {
00035 setting->setValue("0");
00036
00037 MSqlQuery query(MSqlQuery::InitCon());
00038
00039 QString qstr = "SELECT grpid FROM channelgroupnames WHERE name = :GRPNAME";
00040
00041 query.prepare(qstr);
00042 query.bindValue(":GRPNAME", grpname);
00043
00044 if (!query.exec() || !query.isActive())
00045 MythDB::DBError("ChannelGroupStorage::Load", query);
00046 else
00047 {
00048 query.next();
00049 grpid = query.value(0).toUInt();
00050
00051 qstr = "SELECT * FROM channelgroup WHERE grpid = :GRPID AND chanid = :CHANID";
00052 query.prepare(qstr);
00053 query.bindValue(":GRPID", grpid);
00054 query.bindValue(":CHANID", chanid);
00055
00056 if (!query.exec() || !query.isActive())
00057 MythDB::DBError("ChannelGroupStorage::Load", query);
00058 else if (query.size() > 0)
00059 setting->setValue("1");
00060 }
00061 }
00062
00063 void ChannelGroupStorage::Save(void)
00064 {
00065 QString value = setting->getValue();
00066
00067 if (value == "1")
00068 ChannelGroup::AddChannel(chanid, grpid);
00069 else
00070 ChannelGroup::DeleteChannel(chanid, grpid);
00071 }
00072
00073 void ChannelGroupStorage::Save(QString destination)
00074 {
00075 Save();
00076 }
00077
00078 class ChannelCheckBox : public CheckBoxSetting, public ChannelGroupStorage
00079 {
00080 public:
00081 ChannelCheckBox(const ChannelGroupConfig& _parent, const uint chanid, const QString channum,
00082 const QString channame, const QString grpname):
00083 CheckBoxSetting(this),
00084 ChannelGroupStorage(this, chanid, grpname)
00085 {
00086 setLabel(QString("%1 %2").arg(channum).arg(channame));
00087 setHelpText(QObject::tr("Select/Unselect channels for this channel group"));
00088 };
00089 };
00090
00091 ChannelGroupConfig::ChannelGroupConfig(QString _name)
00092 : name(_name)
00093 {
00094 VerticalConfigurationGroup *cgroup;
00095 HorizontalConfigurationGroup *columns;
00096
00097 DBChanList chanlist = ChannelUtil::GetChannels(0, true, "channum, callsign");
00098 ChannelUtil::SortChannels(chanlist, "channum", true);
00099
00100 DBChanList::iterator it = chanlist.begin();
00101 int i,j = 0;
00102 int p = 1;
00103 int pages = (int)((float)chanlist.size() / 8.0 / 3.0 + 0.5);
00104
00105 do
00106 {
00107 columns = new HorizontalConfigurationGroup(false,false,false,false);
00108 columns->setLabel(getName() + " " +
00109 QObject::tr("Channel Group - Page ") + QString("%1").arg(p) +
00110 QObject::tr("of") + QString("%1").arg(pages));
00111
00112 for (j = 0; ((j < 3) && (it < chanlist.end())); ++j)
00113 {
00114 cgroup = new VerticalConfigurationGroup(false,false,false,false);
00115
00116 for (i = 0; ((i < 8) && (it < chanlist.end())); ++i)
00117 {
00118 cgroup->addChild(new ChannelCheckBox(*this, it->chanid, it->channum, it->name, _name));
00119 ++it;
00120 }
00121 columns->addChild(cgroup);
00122 }
00123
00124 ++p;
00125 addChild(columns);
00126 } while (it < chanlist.end());
00127
00128 }
00129
00130 ChannelGroupEditor::ChannelGroupEditor(void) :
00131 listbox(new ListBoxSetting(this)), lastValue("__CREATE_NEW_GROUP__")
00132 {
00133 listbox->setLabel(tr("Channel Groups"));
00134 addChild(listbox);
00135 }
00136
00137 void ChannelGroupEditor::open(QString name)
00138 {
00139 lastValue = name;
00140 bool created = false;
00141
00142 if (name == "__CREATE_NEW_GROUP__")
00143 {
00144 name = "";
00145
00146 bool ok = MythPopupBox::showGetTextPopup(GetMythMainWindow(),
00147 tr("Create New Channel Group"),
00148 tr("Enter group name or press SELECT to enter text via the "
00149 "On Screen Keyboard"), name);
00150 if (!ok)
00151 return;
00152
00153 MSqlQuery query(MSqlQuery::InitCon());
00154 query.prepare("INSERT INTO channelgroupnames (name) VALUES (:NAME);");
00155 query.bindValue(":NAME", name);
00156 if (!query.exec())
00157 MythDB::DBError("ChannelGroupEditor::open", query);
00158 else
00159 created = true;
00160 }
00161
00162 ChannelGroupConfig group(name);
00163
00164 if (group.exec() == QDialog::Accepted || !created)
00165 lastValue = name;
00166
00167 };
00168
00169 void ChannelGroupEditor::doDelete(void)
00170 {
00171 QString name = listbox->getValue();
00172 if (name == "__CREATE_NEW_GROUP__")
00173 return;
00174
00175 QString message = tr("Delete '%1' Channel group?").arg(name);
00176
00177 DialogCode value = MythPopupBox::Show2ButtonPopup(
00178 GetMythMainWindow(),
00179 "", message,
00180 tr("Yes, delete group"),
00181 tr("No, Don't delete group"), kDialogCodeButton1);
00182
00183 if (kDialogCodeButton0 == value)
00184 {
00185 MSqlQuery query(MSqlQuery::InitCon());
00186
00187
00188 query.prepare("SELECT grpid FROM channelgroupnames WHERE name = :NAME;");
00189 query.bindValue(":NAME", name);
00190 if (!query.exec())
00191 MythDB::DBError("ChannelGroupEditor::doDelete", query);
00192 query.next();
00193 uint grpid = query.value(0).toUInt();
00194
00195
00196 query.prepare("DELETE FROM channelgroup WHERE grpid = :GRPID;");
00197 query.bindValue(":GRPID", grpid);
00198 if (!query.exec())
00199 MythDB::DBError("ChannelGroupEditor::doDelete", query);
00200
00201
00202 query.prepare("DELETE FROM channelgroupnames WHERE name = :NAME;");
00203 query.bindValue(":NAME", name);
00204 if (!query.exec())
00205 MythDB::DBError("ChannelGroupEditor::doDelete", query);
00206
00207 lastValue = "__CREATE_NEW_GROUP__";
00208 Load();
00209 }
00210
00211 listbox->setFocus();
00212 }
00213
00214 void ChannelGroupEditor::Load(void)
00215 {
00216 listbox->clearSelections();
00217
00218 ChannelGroupList changrplist;
00219
00220 changrplist = ChannelGroup::GetChannelGroups();
00221
00222 ChannelGroupList::iterator it;
00223
00224 for (it = changrplist.begin(); it < changrplist.end(); ++it)
00225 listbox->addSelection(it->name);
00226
00227 listbox->addSelection(tr("(Create new group)"), "__CREATE_NEW_GROUP__");
00228
00229 listbox->setValue(lastValue);
00230 }
00231
00232 DialogCode ChannelGroupEditor::exec(void)
00233 {
00234 while (ConfigurationDialog::exec() == kDialogCodeAccepted)
00235 open(listbox->getValue());
00236
00237 return kDialogCodeRejected;
00238 }
00239
00240 MythDialog* ChannelGroupEditor::dialogWidget(MythMainWindow* parent,
00241 const char* widgetName)
00242 {
00243 dialog = ConfigurationDialog::dialogWidget(parent, widgetName);
00244 connect(dialog, SIGNAL(menuButtonPressed()), this, SLOT(doDelete()));
00245 connect(dialog, SIGNAL(deleteButtonPressed()), this, SLOT(doDelete()));
00246 return dialog;
00247 }