00001
00024
00025 #include <qmap.h>
00026 #include <qdict.h>
00027 #include <qvaluelist.h>
00028
00029
00030 #include <mythtv/mythcontext.h>
00031
00032
00033 #include "action.h"
00034 #include "actionid.h"
00035 #include "actionset.h"
00036
00037 const QString ActionSet::kJumpContext = "JumpPoints";
00038 const QString ActionSet::kGlobalContext = "Global";
00039
00050 bool ActionSet::Add(const ActionID &id, const QString &key)
00051 {
00052 Action *a = GetAction(id);
00053
00054 if (!a)
00055 return false;
00056
00057 if (!a->AddKey(key))
00058 {
00059 VERBOSE(VB_IMPORTANT, "ActionSet::AddKey() failed");
00060 return false;
00061 }
00062
00063 ActionList &ids = m_keyToActionMap[key];
00064 ids.push_back(id);
00065 SetModifiedFlag(id, true);
00066
00067 return true;
00068 }
00069
00082 bool ActionSet::Remove(const ActionID &id, const QString &key)
00083 {
00084 Action *a = GetAction(id);
00085
00086 if (!a)
00087 return false;
00088
00089 if (!a->RemoveKey(key))
00090 return false;
00091
00092 m_keyToActionMap[key].remove(id);
00093
00094
00095 if (m_keyToActionMap[key].isEmpty())
00096 m_keyToActionMap.remove(key);
00097
00098 SetModifiedFlag(id, true);
00099
00100 return true;
00101 }
00102
00113 bool ActionSet::Replace(const ActionID &id,
00114 const QString &newkey,
00115 const QString &oldkey)
00116 {
00117 Action *a = GetAction(id);
00118
00119 if (!a)
00120 return false;
00121
00122 if (!a->ReplaceKey(newkey, oldkey))
00123 return false;
00124
00125 m_keyToActionMap[oldkey].remove(id);
00126 m_keyToActionMap[newkey].push_back(id);
00127 SetModifiedFlag(id, true);
00128
00129 return true;
00130 }
00131
00136 bool ActionSet::SetModifiedFlag(const ActionID &id, bool modified)
00137 {
00138 if (!modified)
00139 return (m_modified.remove(id) > 0);
00140
00141 if (!IsModified(id))
00142 {
00143 m_modified.push_back(id);
00144 return true;
00145 }
00146
00147 return false;
00148 }
00149
00154 QStringList ActionSet::GetContextStrings(void) const
00155 {
00156 QStringList context_strings;
00157
00158 QDictIterator<Context> it(m_contexts);
00159 for (; it.current(); ++it)
00160 context_strings.append(it.currentKey());
00161
00162 return context_strings;
00163 }
00164
00169 QStringList ActionSet::GetActionStrings(const QString &context_name) const
00170 {
00171 QStringList action_strings;
00172
00173 Context *c = m_contexts[context_name];
00174 if (!c)
00175 return action_strings;
00176
00177 QDictIterator<Action> it(*(m_contexts[context_name]));
00178 for (; it.current(); ++it)
00179 action_strings.append(it.currentKey());
00180
00181 return action_strings;
00182 }
00183
00195 bool ActionSet::AddAction(const ActionID &id,
00196 const QString &description,
00197 const QString &keys)
00198 {
00199 if (!m_contexts[id.GetContext()])
00200 m_contexts.insert(id.GetContext(), new Context());
00201
00202 if ((*m_contexts[id.GetContext()])[id.GetAction()])
00203 return false;
00204
00205 Action *a = new Action(description, keys);
00206 m_contexts[id.GetContext()]->insert(id.GetAction(), a);
00207
00208 const QStringList keylist = a->GetKeys();
00209 QStringList::const_iterator it = keylist.begin();
00210 for (; it != keylist.end(); ++it)
00211 m_keyToActionMap[*it].push_back(id);
00212
00213 return true;
00214 }
00215
00220 QString ActionSet::GetKeyString(const ActionID &id) const
00221 {
00222 const Context *c = m_contexts[id.GetContext()];
00223
00224 if (!c)
00225 return QString::null;
00226
00227 const Action *a = (*c)[id.GetAction()];
00228 if (a)
00229 return a->GetKeyString();
00230
00231 return QString::null;
00232 }
00233
00238 QStringList ActionSet::GetKeys(const ActionID &id) const
00239 {
00240 QStringList keys;
00241
00242 const Context *c = m_contexts[id.GetContext()];
00243 if (c)
00244 {
00245 const Action *a = (*c)[id.GetAction()];
00246 if (a)
00247 keys = a->GetKeys();
00248 }
00249
00250 return keys;
00251 }
00252
00253 QStringList ActionSet::GetContextKeys(const QString & context_name) const
00254 {
00255 QStringList keys;
00256 Context *c = m_contexts[context_name];
00257
00258 QDictIterator<Action> it(*c);
00259 for (;it.current(); ++it)
00260 {
00261 QStringList akeys = (*it)->GetKeys();
00262 for (size_t i = 0; i < akeys.size(); i++)
00263 {
00264 keys.append(akeys[i]);
00265 }
00266 keys.sort();
00267 }
00268
00269 return keys;
00270 }
00271
00274 QStringList ActionSet::GetAllKeys(void) const
00275 {
00276 QStringList keys;
00277
00278 QMap<QString, ActionList>::ConstIterator it;
00279
00280 for (it = m_keyToActionMap.begin(); it != m_keyToActionMap.end(); ++it)
00281 keys.push_back(it.key());
00282
00283 return keys;
00284 }
00285
00290 QString ActionSet::GetDescription(const ActionID &id) const
00291 {
00292 const Context *c = m_contexts[id.GetContext()];
00293 if (!c)
00294 return QString::null;
00295
00296 const Action *a = (*c)[id.GetAction()];
00297 if (a)
00298 return a->GetDescription();
00299
00300 return QString::null;
00301 }
00302
00307 const ActionList &ActionSet::GetActions(const QString &key) const
00308 {
00309 return m_keyToActionMap[key];
00310 }
00311
00316 Action *ActionSet::GetAction(const ActionID &id) const
00317 {
00318 Context *ctx = m_contexts[id.GetContext()];
00319 if (!ctx)
00320 {
00321 VERBOSE(VB_IMPORTANT, QString("GetAction: Did not find context '%1'")
00322 .arg(id.GetContext()));
00323
00324 return NULL;
00325 }
00326
00327 Action *action = (*ctx)[id.GetAction()];
00328
00329 if (!action)
00330 {
00331 VERBOSE(VB_IMPORTANT, QString("GetAction: Did not find action '%1' "
00332 "in context '%1'")
00333 .arg(id.GetAction()).arg(id.GetContext()));
00334 }
00335
00336 return action;
00337 }
00338
00343 Context *ActionSet::GetContext(const QString &name) const
00344 {
00345 return m_contexts[name];
00346 }