File: plugin.cpp

package info (click to toggle)
madman 0.93.0-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,060 kB
  • ctags: 3,309
  • sloc: cpp: 14,194; ansic: 13,331; sh: 100; makefile: 62; python: 54
file content (285 lines) | stat: -rw-r--r-- 7,442 bytes parent folder | download
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
/*
madman - a music manager
Copyright (C) 2003  Andreas Kloeckner <ak@ixion.net>

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.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/




#include "utility/plugin.h"
#include "database/song.h"

#include <qfile.h>
#include <qfileinfo.h>
#include <qdir.h>
#include <qapplication.h>
#include <qregexp.h>
#include <qsettings.h>
#include <qinputdialog.h>

#include <stdexcept>
#include <csignal>
#include <sys/wait.h>




// tPlugin --------------------------------------------------------------------
tPlugin::tPlugin()
  : Confirm(false), RescanAfter(false)
{
}




bool tPlugin::gatherInfo()
{
  Confirm = false;
  RescanAfter = false;
  
  QRegExp this_is_not_re("MADMAN-THIS-IS-NOT-A-PLUGIN");
  QRegExp version_re("MADMAN-PLUGIN-VERSION:(.*)$");
  QRegExp name_re("MADMAN-NAME:(.*)$");
  QRegExp description_re("MADMAN-DESCRIPTION:(.*)$");
  QRegExp arguments_re("MADMAN-ARGUMENTS:(.*)$");
  QRegExp menustring_re("MADMAN-MENUSTRING:(.*)$");
  QRegExp shortcut_re("MADMAN-KEYBOARDSHORTCUT:(.*)$");
  QRegExp confirm_re("MADMAN-CONFIRM:(.*)$");
  QRegExp rescanafter_re("MADMAN-RESCANAFTER:(.*)$");
  QFile pluginfile(Filename);
  if (!pluginfile.open(IO_ReadOnly))
    return false;

  bool isplugin = false;

  QTextStream stream(&pluginfile);
  while (!stream.eof())
  {
    QString line = stream.readLine();

    if (this_is_not_re.search(line) >= 0)
      return false;

    if (version_re.search(line) >= 0)
    { 
      isplugin = version_re.cap(1).stripWhiteSpace() == "2";
      if (!isplugin)
	return false;
    }
    if (name_re.search(line) >= 0)
    { 
      Name = name_re.cap(1).stripWhiteSpace();
    }
    else if (description_re.search(line) >= 0)
    { 
      Description = description_re.cap(1).stripWhiteSpace();
    }
    else if (arguments_re.search(line) >= 0)
    { 
      Arguments = arguments_re.cap(1).stripWhiteSpace();
    }
    else if (menustring_re.search(line) >= 0)
    { 
      MenuString = menustring_re.cap(1).stripWhiteSpace();
    }
    else if (shortcut_re.search(line) >= 0)
    { 
      KeyboardShortcut = shortcut_re.cap(1).stripWhiteSpace();
    }
    else if (confirm_re.search(line) >= 0)
    { 
      bool ok;
      Confirm = confirm_re.cap(1).stripWhiteSpace().toInt(&ok) != 0;
      if (!ok)
	throw runtime_error("found invalid numeral in confirm line of "+QString2string(Filename));
    }
    else if (rescanafter_re.search(line) >= 0)
    { 
      bool ok;
      RescanAfter = rescanafter_re.cap(1).stripWhiteSpace().toInt(&ok) != 0;
      if (!ok)
	throw runtime_error("found invalid numeral in rescanafter line of "+QString2string(Filename));
    }
  }

  if (MenuString.isNull())
    MenuString = Name;

  return isplugin;
}




void tPlugin::configure (QSettings &settings)
{
  QFileInfo info(Filename);

  QRegExp config_re("%config:(.*)%");
  config_re.setMinimal(true);
  int where = 0;
  while ((where = config_re.search(Arguments, where + 1)) >= 0)
  {
    QString prompt = config_re.cap(1);
    QString key = QString("/madman/plugins/%1/%2").arg(info.fileName()).arg(prompt);
    QString value = settings.readEntry(key);

    bool ok;
    QString input = QInputDialog::getText(qApp->translate("tPlugin", "madman Plugin"),
	prompt, QLineEdit::Normal, value, &ok );
    if (!ok)
      return;
    settings.writeEntry(key, input);
  }
}




namespace
{
  QString expand(const QString &pluginname, QSettings &settings, const QString &victim, tSong *context)
  {
    QString result = victim;
    if (context)
      result = substituteSongFields(result, context, false, true);

    QRegExp ask_re("%ask:(.*)%");
    ask_re.setMinimal(true);
    int where = 0;
    while ((where = ask_re.search(result)) >= 0)
    {
      QString prompt = ask_re.cap(1);
      bool ok;
      QString input = QInputDialog::getText(qApp->translate("tPlugin", "madman Plugin"),
	  prompt, QLineEdit::Normal, QString::null, &ok );

      result.replace(where, ask_re.matchedLength(), QString("\"%1\"").arg(input));
    }

    QFileInfo info(pluginname);

    QRegExp config_re("%config:(.*)%");
    config_re.setMinimal(true);
    while ((where = config_re.search(result)) >= 0)
    {
      bool ok;
      QString key = QString("/madman/plugins/%1/%2").arg(info.fileName()).arg(config_re.cap(1));
      QString value = settings.readEntry(key, QString::null, &ok);
      if (!ok)
	throw tRuntimeError(qApp->translate("tPlugin", "Plugin not yet configured"));

      result.replace(where, config_re.matchedLength(), QString("\"%1\"").arg(value));
    }

    return result;
  }




  void mySystem(const QString &command)
  {
    int ret = system(command.latin1());
    /*
     * XXX FIXME portability problems to FreeBSD
    if (WIFSIGNALED(ret) &&
	(WTERMSIG(ret) == SIGINT || WTERMSIG(ret) == SIGQUIT))
      throw tRuntimeError(qApp->translate("tPlugin", "Plugin died because it caught SIGINT or SIGQUIT "));
      */
    if (ret)
      throw tRuntimeError(qApp->translate("tPlugin", "Plugin execution resulted in non-zero return value"));
  }
}




void tPlugin::run(QSettings &settings, const tSongList &songs)
{
  QString myargs = Arguments;
  QRegExp repeat_re("%repeat%(.*)%endrepeat%");
  bool hasrepeat = repeat_re.search(myargs) >= 0;
  if (hasrepeat)
  {
    QString repeat_section = repeat_re.cap(1);
    myargs = myargs.replace(repeat_re, "%REPEAT%");

    myargs = expand(Filename, settings, myargs, NULL);
    if (myargs.isNull())
      return;

    QString repeatargs;
    FOREACH_CONST(first, songs, tSongList)
    {
      QString expansion = expand(Filename, settings, repeat_section, *first);
      if (expansion.isNull())
	return;
      repeatargs += " " + expansion;
    }

    myargs.replace("%REPEAT%", repeatargs);

    QString command = Filename + " " + myargs;
    mySystem(command);
  }
  else
  {
    FOREACH_CONST(first, songs, tSongList)
    {
      QString expansion = expand(Filename, settings, myargs, *first);
      if (expansion.isNull())
	return;
      QString command = Filename + " " + expansion;
      mySystem(command);
    }
  }
}




// functions ------------------------------------------------------------------
void enumeratePlugins(const tDirectoryList &dir_list, tPluginList &plugin_list)
{
  FOREACH_CONST(first, dir_list, tDirectoryList)
  {
    QDir directory(QString::fromUtf8(first->c_str()));
    if (!directory.exists())
    {
      cerr << "[ERROR] nonexistent: " << *first << endl;
      continue;
    }

    QStringList strlist = directory.entryList();
    for (unsigned i = 0; i < strlist.size(); i++)
    {
      QString name = strlist[ i ];
      QString fullname = QString::fromUtf8(first->c_str()) + "/" + name;

      QFileInfo file(fullname);
      if (!file.isExecutable())
	continue;

      tPlugin plugin;
      plugin.Filename = fullname;
      if (plugin.gatherInfo())
	plugin_list.push_back(plugin);
    }
  }
}