summaryrefslogtreecommitdiffstats
path: root/quanta/utility/qpevents.cpp
blob: b4d43f48a49ebb9a98b337297c5f033f9cc94236 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/***************************************************************************
                          qpevents.cpp  -  description
                             -------------------
    begin                : Sun Jul 11 2004
    copyright          : (C) 2004 Andras Mantia <amantia@kde.org>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

//qt includes
#include <tqdatetime.h>
#include <tqfile.h>
#include <tqtextstream.h>

//kde includes
#include <kapplication.h>
#include <kdebug.h>
#include <klocale.h>
#include <kmessagebox.h>

//app includes
#include "qpevents.h"
#include "document.h"
#include "project.h"
#include "qextfileinfo.h"
#include "viewmanager.h"
#include "resource.h"
#include "quanta.h"
#include "tagaction.h"

//TODO: Better create a class for each internal event action
QPEvents::QPEvents(TQObject *parent, const char *name)
 : TQObject(parent, name)
{
   m_eventNames["before_save"] = i18n("Before Document Save");
   m_eventNames["after_save"] = i18n("After Document Save");
   m_eventNames["after_open"] = i18n("After Document Open");
   m_eventNames["before_close"] = i18n("Before Document Close");
   m_eventNames["after_close"] = i18n("After Document Close");
   m_eventNames["after_project_open"] = i18n("After Project Open");
   m_eventNames["before_project_close"] = i18n("Before Project Close");
   m_eventNames["after_project_close"] = i18n("After Project Close");
   m_eventNames["upload_requested"] = i18n("Upload Requested");
   m_eventNames["before_upload"] = i18n("Before Document Upload");
   m_eventNames["after_upload"] = i18n("After Document Upload");
   m_eventNames["after_project_add"] = i18n("After Addition to Project");
   m_eventNames["after_project_remove"] = i18n("After Removal From Project");
   m_eventNames["after_commit"] = i18n("After Commit to CVS");
   m_eventNames["after_update"] = i18n("After Update From CVS");
   m_eventNames["after_file_move"] = i18n("After Moving File Inside Project");
   m_eventNames["quanta_start"] = i18n("Quanta Start");
   m_eventNames["quanta_exit"] = i18n("Quanta Exit");
//   m_eventNames["after_multiple_save"] = i18n("After saving more files at once (like Save All)");

   m_actionNames["email"] = i18n("Send Email");
   m_actionNames["log"] = i18n("Log Event");
   m_actionNames["script"] = i18n("Script Action");
   m_actionNames["action"] = i18n("Non-Script Action");
}


QPEvents::~QPEvents()
{
}

void QPEvents::slotEventHappened(const TQString& name, const TQString& argument1, const TQString& argument2)
{
  if (!quantaApp || !Project::ref()->eventsEnabled())
    return;
  EventActions *events = Project::ref()->events();
  if (!events) return;
  if (events->contains(name))
  {
    m_eventName = name;
    TQValueList<EventAction> evList = (*events)[name];
    for (TQValueList<EventAction>::Iterator it = evList.begin(); it != evList.end(); ++it)
    {
      EventAction ev = *it;
      if (ev.type == EventAction::Internal)
      {
        if (KMessageBox::warningContinueCancel(0L, i18n("<qt>An internal action (<i>%1</i>) associated with an event (<i>%2</i>) will be executed.  Do you want to allow the execution of this action?</qt>").tqarg(ev.action).tqarg(name), i18n("Event Triggered"), i18n("Execute"), "Warn about internal actions") == KMessageBox::Cancel)
          return;
      } else
      {
        if (KMessageBox::warningContinueCancel(0L, i18n("<qt>An external action (<i>%1</i>) associated with an event (<i>%2</i>) will be executed.  Do you want to allow the execution of this action?</qt>").tqarg(ev.action).tqarg(name), i18n("Event Triggered"), i18n("Execute"), "Warn about external actions") == KMessageBox::Cancel)
          return;
      }
      KURL url = KURL::fromPathOrURL(argument1);
      KURL url2 = KURL::fromPathOrURL(argument2);
      if (url.isValid())
      {
        bool inProject = Project::ref()->contains(url);
        if (inProject)
        {
          if (name == "upload_requested")
            {
              ev.arguments << i18n("An upload was initiated");
              ev.arguments << url.path();
              handleEvent(ev);
            }
        }
        if (inProject && url2.isValid())
        {
            if (name == "before_upload")
            {
              ev.arguments << i18n("About to upload a document");
              ev.arguments << url.path();
              ev.arguments << url2.path();
              handleEvent(ev);
            } else
            if (name == "after_upload")
            {
              ev.arguments << i18n("Document uploaded");
              ev.arguments << url.path();
              ev.arguments << url2.path();
              handleEvent(ev);
            } else
            if (name == "after_file_move")
            {
              ev.arguments << i18n("Document moved");
              ev.arguments << url.path();
              ev.arguments << url2.path();
              handleEvent(ev);
            }
        } else
        {
            TQString relativePath = QExtFileInfo::toRelative(url, Project::ref()->projectBaseURL()).path();
            if (inProject && name == "after_save")
            {
              ev.arguments << i18n("Document saved");
              ev.arguments << relativePath;
              handleEvent(ev);
            } else
            if (inProject && name == "before_save")
            {
              ev.arguments << i18n("About to save a document");
              ev.arguments << relativePath;
              handleEvent(ev);
            } else
            if (inProject && name == "after_open")
            {
              ev.arguments << i18n("Document opened");
              ev.arguments << relativePath;
              handleEvent(ev);
            } else
            if (inProject && name == "after_close")
            {
              ev.arguments << i18n("Document closed");
              ev.arguments << relativePath;
              handleEvent(ev);
            } else
            if (inProject && name == "before_close")
            {
              ev.arguments << i18n("About to close a document");
              ev.arguments << relativePath;
              handleEvent(ev);
            } else
            if (name == "after_project_open")
            {
                ev.arguments << i18n("Project opened");
                ev.arguments <<  url.path();
                handleEvent(ev);
            } else
            if (name == "after_project_close")
            {
                ev.arguments << i18n("Project closed");
                ev.arguments <<  url.path();
                handleEvent(ev);
            } else
            if (name == "before_project_close")
            {
                ev.arguments << i18n("About to close the project");
                ev.arguments <<  url.path();
                handleEvent(ev);
            } else
            if (name == "after_project_add")
            {
                ev.arguments << i18n("Document added to project");
                ev.arguments <<  url.path();
                handleEvent(ev);
            } else
            if (name == "after_project_remove")
            {
                ev.arguments << i18n("Document removed from project");
                ev.arguments <<  url.path();
                handleEvent(ev);
            }
        }
      } else
      if (name == "after_commit")
      {
        ev.arguments << i18n("Document committed");
        ev.arguments << argument1;
        handleEvent(ev);
      } else
      if (name == "after_update")
      {
        ev.arguments << i18n("Document updated");
        ev.arguments << argument1;
        handleEvent(ev);
      } else
      if (name == "quanta_start")
      {
        ev.arguments << i18n("Quanta has been started");
        ev.arguments << argument1;
        handleEvent(ev);
      } else
      if (name == "quanta_exit")
      {
        ev.arguments << i18n("Quanta is shutting down");
        ev.arguments << argument1;
        handleEvent(ev);
      }
    }
  }
  if (!m_eventNames.contains(name))
    KMessageBox::sorry(0L, i18n("<qt>Unsupported event <b>%1</b>.</qt>").tqarg(name), i18n("Event Handling Error"));
}

bool QPEvents::handleEvent(const EventAction& ev)
{
  if (ev.type == EventAction::Internal)
  {
    if (ev.action == "email")
    {
       TQString receiver = ev.arguments[0];
       TeamMember member;
       if (receiver == "teamleader")
         member = Project::ref()->teamLeader();
       else if (receiver.startsWith("subprojectleader-"))
       {
         TQString s = receiver.remove("subprojectleader-");
         member = Project::ref()->subprojectLeader(s);
         SubProject subProject;
         TQValueList<SubProject> *subprojects = Project::ref()->subprojects();
         for (uint i = 0 ; i < subprojects->count(); i++)
         {
           if ((*subprojects)[i].name == s)
           {
              subProject = (*subprojects)[i];
              break;
           }
         }
         if (!subProject.location.isEmpty() && !ev.arguments[2].startsWith(subProject.location))
         {
            kdDebug(24000) << ev.arguments[2] << " is not part of the " << subProject.name << "subproject \"" << subProject.location << "\". " << endl;
            return true;
         }
       }
       else if (receiver.startsWith("taskleader-"))
         member = Project::ref()->taskLeader(receiver.remove("taskleader-"));

       TQString body;
       for (uint i = 2; i < ev.arguments.count(); i++)
          body += ev.arguments[i] + "\n";
       kapp->invokeMailer(member.name + "<" + member.email + ">", "", "", ev.arguments[1], body, "", TQStringList(), "");

       return true;
    }
    if (ev.action == "log")
    {
      TQString logFile = ev.arguments[0];
      KURL url = KURL::fromPathOrURL(logFile);
      if (url.isValid() && !url.isLocalFile())
      {
        KMessageBox::sorry(0L, i18n("Logging to remote files is not supported."));
        return false;
      }
      if (!logFile.startsWith("/"))
      {
        url = Project::ref()->projectBaseURL();
        url.addPath(logFile);
        if (!url.isLocalFile())
        {
          KMessageBox::sorry(0L, i18n("Logging to files inside a remote project is not supported."));
          return false;
        }
      }
      TQFile file(url.path());
      bool result;
      if (ev.arguments[2] == "create_new")
        result = file.open(IO_WriteOnly);
      else
        result = file.open(IO_WriteOnly | IO_Append);
      if (result)
      {
        TQTextStream stream(&file);
        stream.setEncoding(TQTextStream::UnicodeUTF8);
        //Note: the log text should not be translated.
        TQString s = TQDateTime::tqcurrentDateTime().toString(Qt::ISODate) + ": ";
        s.append( "Event : " + m_eventName + " : ");
        s.append( "Action: " + ev.action + " : ");
        if (ev.arguments[1] == "full")
        {
          s.append( "Arguments: ");
          for (uint i = 1; i < ev.arguments.count(); i++)
            s.append(ev.arguments[i] + " | ");
        }
        s[s.length() - 1] = '\n';
        stream << s;
        file.close();
      }
      if (!result)
      {
        KMessageBox::sorry(0L, i18n("<qt>Logging failed. Check that you have write access to <i>%1</i>.").tqarg(url.path()));
        return false;
      }
    } else
      KMessageBox::sorry(0L, i18n("<qt>Unsupported internal event action : <b>%1</b>.</qt>").tqarg(ev.action));
  } else
  if (ev.type == EventAction::External)
  {
      //KMessageBox::sorry(0L, i18n("External event actions are not yet supported."));
      if (ev.action == "script" || ev.action =="action")
      {
          TQString name = ev.arguments[0];
          KAction *action = quantaApp->actionCollection()->action(name.ascii());
          TagAction *tagAction = dynamic_cast<TagAction*>(action);
          if (tagAction)
          {
            bool blocking = (ev.arguments[1] == "yes");
            EventAction event = ev;
            event.arguments.remove(event.arguments.at(1));
            tagAction->addArguments(event.arguments);
            tagAction->execute(blocking);
          }
          else
          if (action)
          {
            action->activate();
          } else
            KMessageBox::sorry(0L, i18n("<qt>The <b>%1</b> script action was not found on your system.</qt>").tqarg(name), i18n("Action Execution Error"));
      }  else
      KMessageBox::sorry(0L, i18n("Unsupported external event action."));
  } else
      KMessageBox::sorry(0L, i18n("Unknown event type."));
  return false;
}

TQString QPEvents::fullEventName(const TQString &name)
{
   if (m_eventNames.contains(name))
     return m_eventNames[name];
   else
     return name;
}

TQString QPEvents::fullActionName(const TQString& name)
{
   if (m_actionNames.contains(name))
     return m_actionNames[name];
   else
     return name;
}

TQString QPEvents::eventName(const TQString &fullName)
{
   for (TQMap<TQString, TQString>::ConstIterator it = m_eventNames.constBegin(); it != m_eventNames.constEnd(); ++it)
  {
    if (fullName  == it.data())
      return it.key();
  }
  return fullName;
}

TQString QPEvents::actionName(const TQString &fullName)
{
   for (TQMap<TQString, TQString>::ConstIterator it = m_actionNames.constBegin(); it != m_actionNames.constEnd(); ++it)
  {
    if (fullName  == it.data())
      return it.key();
  }
  return fullName;
}

TQStringList QPEvents::eventNames()
{
   TQStringList names;
   for (TQMap<TQString, TQString>::ConstIterator it = m_eventNames.constBegin(); it != m_eventNames.constEnd(); ++it)
  {
    names << it.data();
  }
  return names;
}

TQStringList QPEvents::actionNames()
{
   TQStringList names;
   for (TQMap<TQString, TQString>::ConstIterator it = m_actionNames.constBegin(); it != m_actionNames.constEnd(); ++it)
  {
    names << it.data();
  }
  return names;
}

#include "qpevents.moc"