Crossfire Server, Trunk  R20513
MessageManager.cpp
Go to the documentation of this file.
1 #include "MessageManager.h"
2 #include "MessageFile.h"
3 #include "QuestConditionScript.h"
4 
5 extern "C" {
6  #include "global.h"
7 }
8 
9 #include <QDir>
10 #include <QDebug>
11 
13 {
14 }
15 
17 {
18  qDeleteAll(myMessages);
19  qDeleteAll(myPreConditions);
20  qDeleteAll(myPostConditions);
21 }
22 
24 {
25  loadDirectory("");
26 
27  /* get pre and post conditions */
30 }
31 
33 {
34  foreach(MessageFile* file, myMessages)
35  {
36  file->save();
37  }
38 }
39 
40 QList<MessageFile*>& MessageManager::messages()
41 {
42  return myMessages;
43 }
44 
45 const QList<MessageFile*>& MessageManager::messages() const
46 {
47  return myMessages;
48 }
49 
51 {
52  foreach(MessageFile* file, myMessages)
53  {
54  if (file->path() == path)
55  return file;
56  }
57 
58  return NULL;
59 }
60 
61 void MessageManager::loadDirectory(const QString& directory)
62 {
63  //qDebug() << "load" << directory;
64  QDir dir(QString("%1/%2/%3").arg(settings.datadir, settings.mapdir, directory));
65 
66  // first messages
67  QStringList messages = dir.entryList(QStringList("*.msg"), QDir::Files);
68  //qDebug() << "found" << messages;
69  foreach(QString message, messages)
70  {
71  QString path = directory + QDir::separator() + message;
72  MessageFile* file = new MessageFile(path);
73  if (file->parseFile())
74  {
75  myMessages.append(file);
76  }
77  else
78  {
79  qDebug() << "dialog parse error" << path;
80  delete file;
81  }
82  }
83 
84  // recurse
85  QStringList subdirs = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
86  foreach(QString sub, subdirs)
87  loadDirectory(directory + QDir::separator() + sub);
88 }
89 
90 QList<QuestConditionScript*> MessageManager::preConditions() const
91 {
92  return myPreConditions;
93 }
94 
95 QList<QuestConditionScript*> MessageManager::postConditions() const
96 {
97  return myPostConditions;
98 }
99 
100 QString MessageManager::loadScriptComment(const QString& path) const
101 {
102  QFile file(path);
103  if (!file.open(QIODevice::ReadOnly))
104  return "";
105 
106  QTextStream stream(&file);
107  QStringList lines = stream.readAll().split("\n");
108 
109  QString comment, line;
110 
111  /* by convention, the first 2 lines are encoding and script name */
112  for(int i = 2; i < lines.size(); i++)
113  {
114  line = lines[i];
115  if (!line.startsWith("# "))
116  break;
117  comment += line.mid(2) + "\n";
118  }
119 
120  return comment.trimmed();
121 }
122 
123 void MessageManager::findPrePost(const QString directory, QList<QuestConditionScript*>& list)
124 {
125  QDir dir(QString("%1/%2/python/dialog/%3").arg(settings.datadir, settings.mapdir, directory));
126  QFileInfoList files = dir.entryInfoList(QStringList("*.py"));
127  foreach(QFileInfo file, files)
128  {
129  list.append(new QuestConditionScript(file.baseName(), loadScriptComment(file.absoluteFilePath())));
130  }
131 }
void loadDirectory(const QString &directory)
QList< MessageFile * > & messages()
void findPrePost(const QString directory, QList< QuestConditionScript * > &list)
QList< MessageFile * > myMessages
Global type definitions and header inclusions.
MessageFile * findMessage(const QString &path)
virtual ~MessageManager()
QList< QuestConditionScript * > myPreConditions
QList< QuestConditionScript * > preConditions() const
const char * datadir
Read only data files.
Definition: global.h:244
const char * mapdir
Where the map files are.
Definition: global.h:247
struct Settings settings
Server settings.
Definition: init.c:40
QList< QuestConditionScript * > myPostConditions
QList< QuestConditionScript * > postConditions() const
bool parseFile()
const QString & path() const
QString loadScriptComment(const QString &path) const