00001
00024
00025 #include "mythlogging.h"
00026
00027
00028 #include "action.h"
00029 #include "actionset.h"
00030
00031 const QString ActionSet::kJumpContext = "JumpPoints";
00032 const QString ActionSet::kGlobalContext = "Global";
00033
00034 ActionSet::~ActionSet()
00035 {
00036 while (!m_contexts.empty())
00037 {
00038 Context &ctx = *m_contexts.begin();
00039 while (!ctx.empty())
00040 {
00041 delete *ctx.begin();
00042 ctx.erase(ctx.begin());
00043 }
00044 m_contexts.erase(m_contexts.begin());
00045 }
00046 }
00047
00058 bool ActionSet::Add(const ActionID &id, const QString &key)
00059 {
00060 Action *a = GetAction(id);
00061
00062 if (!a)
00063 return false;
00064
00065 if (!a->AddKey(key))
00066 {
00067 LOG(VB_GENERAL, LOG_ERR, "ActionSet::AddKey() failed");
00068 return false;
00069 }
00070
00071 ActionList &ids = m_keyToActionMap[key];
00072 ids.push_back(id);
00073 SetModifiedFlag(id, true);
00074
00075 return true;
00076 }
00077
00090 bool ActionSet::Remove(const ActionID &id, const QString &key)
00091 {
00092 Action *a = GetAction(id);
00093
00094 if (!a)
00095 return false;
00096
00097 if (!a->RemoveKey(key))
00098 return false;
00099
00100 m_keyToActionMap[key].removeAll(id);
00101
00102
00103 if (m_keyToActionMap[key].isEmpty())
00104 m_keyToActionMap.remove(key);
00105
00106 SetModifiedFlag(id, true);
00107
00108 return true;
00109 }
00110
00121 bool ActionSet::Replace(const ActionID &id,
00122 const QString &newkey,
00123 const QString &oldkey)
00124 {
00125 Action *a = GetAction(id);
00126
00127 if (!a)
00128 return false;
00129
00130 if (!a->ReplaceKey(newkey, oldkey))
00131 return false;
00132
00133 m_keyToActionMap[oldkey].removeAll(id);
00134 m_keyToActionMap[newkey].push_back(id);
00135 SetModifiedFlag(id, true);
00136
00137 return true;
00138 }
00139
00144 bool ActionSet::SetModifiedFlag(const ActionID &id, bool modified)
00145 {
00146 if (!modified)
00147 return m_modified.removeAll(id);
00148
00149 if (!IsModified(id))
00150 {
00151 m_modified.push_back(id);
00152 return true;
00153 }
00154
00155 return false;
00156 }
00157
00161 QStringList ActionSet::GetContextStrings(void) const
00162 {
00163 QStringList context_strings;
00164
00165 ContextMap::const_iterator it = m_contexts.begin();
00166 for (; it != m_contexts.end(); ++it)
00167 context_strings.append(it.key());
00168
00169 context_strings.detach();
00170 return context_strings;
00171 }
00172
00176 QStringList ActionSet::GetActionStrings(const QString &context_name) const
00177 {
00178 QStringList action_strings;
00179
00180 ContextMap::const_iterator cit = m_contexts.find(context_name);
00181 if (cit == m_contexts.end())
00182 return action_strings;
00183
00184 Context::const_iterator it = (*cit).begin();
00185 for (; it != (*cit).end(); ++it)
00186 action_strings.append(it.key());
00187
00188 action_strings.detach();
00189 return action_strings;
00190 }
00191
00203 bool ActionSet::AddAction(const ActionID &id,
00204 const QString &description,
00205 const QString &keys)
00206 {
00207 ContextMap::iterator cit = m_contexts.find(id.GetContext());
00208 if (cit == m_contexts.end())
00209 cit = m_contexts.insert(id.GetContext(), Context());
00210 else if ((*cit).find(id.GetAction()) != (*cit).end())
00211 return false;
00212
00213 Action *a = new Action(description, keys);
00214 (*cit).insert(id.GetAction(), a);
00215
00216 const QStringList keylist = a->GetKeys();
00217 QStringList::const_iterator it = keylist.begin();
00218 for (; it != keylist.end(); ++it)
00219 m_keyToActionMap[*it].push_back(id);
00220
00221 return true;
00222 }
00223
00228 QString ActionSet::GetKeyString(const ActionID &id) const
00229 {
00230 ContextMap::const_iterator cit = m_contexts.find(id.GetContext());
00231 if (cit == m_contexts.end())
00232 return QString();
00233
00234 Context::const_iterator it = (*cit).find(id.GetAction());
00235 if (it != (*cit).end())
00236 return (*it)->GetKeyString();
00237
00238 return QString();
00239 }
00240
00244 QStringList ActionSet::GetKeys(const ActionID &id) const
00245 {
00246 QStringList keys;
00247
00248 ContextMap::const_iterator cit = m_contexts.find(id.GetContext());
00249 if (cit == m_contexts.end())
00250 return keys;
00251
00252 Context::const_iterator it = (*cit).find(id.GetAction());
00253 if (it != (*cit).end())
00254 keys = (*it)->GetKeys();
00255
00256 keys.detach();
00257 return keys;
00258 }
00259
00260 QStringList ActionSet::GetContextKeys(const QString &context_name) const
00261 {
00262 QStringList keys;
00263 ContextMap::const_iterator cit = m_contexts.find(context_name);
00264 if (cit == m_contexts.end())
00265 return keys;
00266
00267 Context::const_iterator it = (*cit).begin();
00268 for (; it != (*cit).end(); ++it)
00269 keys += (*it)->GetKeys();
00270 keys.sort();
00271
00272 keys.detach();
00273 return keys;
00274 }
00275
00278 QStringList ActionSet::GetAllKeys(void) const
00279 {
00280 QStringList keys;
00281
00282 QMap<QString, ActionList>::ConstIterator it;
00283
00284 for (it = m_keyToActionMap.begin(); it != m_keyToActionMap.end(); ++it)
00285 keys.push_back(it.key());
00286
00287 return keys;
00288 }
00289
00293 QString ActionSet::GetDescription(const ActionID &id) const
00294 {
00295 ContextMap::const_iterator cit = m_contexts.find(id.GetContext());
00296 if (cit == m_contexts.end())
00297 return QString();
00298
00299 Context::const_iterator it = (*cit).find(id.GetAction());
00300 if (it != (*cit).end())
00301 return (*it)->GetDescription();
00302
00303 return QString();
00304 }
00305
00309 ActionList ActionSet::GetActions(const QString &key) const
00310 {
00311 ActionList list = m_keyToActionMap[key];
00312 list.detach();
00313 return list;
00314 }
00315
00320 ActionList ActionSet::GetModified(void) const
00321 {
00322 ActionList list = m_modified;
00323 list.detach();
00324 return list;
00325 }
00326
00331 Action *ActionSet::GetAction(const ActionID &id)
00332 {
00333 ContextMap::iterator cit = m_contexts.find(id.GetContext());
00334 if (cit == m_contexts.end())
00335 {
00336 LOG(VB_GENERAL, LOG_ERR,
00337 QString("GetAction: Did not find context '%1'")
00338 .arg(id.GetContext()));
00339
00340 return NULL;
00341 }
00342
00343 Context::iterator it = (*cit).find(id.GetAction());
00344
00345 if (it == (*cit).end())
00346 {
00347 LOG(VB_GENERAL, LOG_ERR,
00348 QString("GetAction: Did not find action '%1' in context '%1'")
00349 .arg(id.GetAction()).arg(id.GetContext()));
00350 return NULL;
00351 }
00352
00353 return *it;
00354 }