File indexing completed on 2024-04-14 04:00:00

0001 //
0002 // C++ Implementation: ctriggerlist
0003 //
0004 // Description: 
0005 //
0006 /*
0007 Copyright 2002-2011 Tomas Mecir <kmuddy@kmuddy.com>
0008 
0009 This program is free software; you can redistribute it and/or
0010 modify it under the terms of the GNU General Public License as
0011 published by the Free Software Foundation; either version 2 of 
0012 the License, or (at your option) any later version.
0013 
0014 This program is distributed in the hope that it will be useful,
0015 but WITHOUT ANY WARRANTY; without even the implied warranty of
0016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017 GNU General Public License for more details.
0018 
0019 You should have received a copy of the GNU General Public License
0020 along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021 */
0022 
0023 #include "ctriggerlist.h"
0024 
0025 #include "cactionmanager.h"
0026 #include "ccmdqueue.h"
0027 #include "ccmdqueues.h"
0028 #include "cpattern.h"
0029 #include "ctextchunk.h"
0030 #include "ctrigger.h"
0031 #include "ctriggereditor.h"
0032 
0033 #include <kmessagebox.h>
0034 #include <KLocalizedString>
0035 
0036 //used in loop detection
0037 #define MAXTRIGGERS 50
0038 
0039 struct cTriggerList::Private {
0040   bool detectingPrompt;
0041 
0042   cTextChunk *curline;
0043   QString plainText;
0044 };
0045 
0046 cTriggerList::cTriggerList ()
0047   : cList ("triggers")
0048 {
0049   // create properties
0050   // basic ones
0051   addStringProperty ("pattern", "Matching pattern");
0052   addIntProperty ("newtext-count", "Replacement line count", 0);
0053   // then we have a "newtext-"+i string for each
0054   addIntProperty ("matching", "Comparison type", int(cPattern::substring));
0055   addStringProperty ("condition", "Matching condition");
0056 
0057   // script
0058   addStringProperty ("script", "Script to execute");
0059 
0060   // options
0061   addBoolProperty ("cs", "Case sensitive", true);
0062   addBoolProperty ("dont-send", "Don't send", false);
0063   addBoolProperty ("whole-words", "Whole words", true);
0064   addBoolProperty ("global", "Global matching", false);
0065   addIntProperty ("action-matched", "Action if matched", (int) cList::Stop);
0066   addIntProperty ("action-not-matched", "Action if not matched", (int) cList::Continue);
0067 
0068   // colorizations
0069   addBoolProperty ("colorize", "Color trigger", false);
0070   addIntProperty ("colorize-count", "Colorization count", 0);
0071   // then we have a "colorize-variable-"+i - which variable is affected
0072   // "colorize-fg-"+i - foreground color
0073   // "colorize-bg-"+i - background color
0074   // the FG and BG numbers: positive means a RGB value (subtract 1 to get real value), negative means a color from the ANSI set (add 16 to get the real color), 0 means no change
0075 
0076   // special triggers
0077   addBoolProperty ("rewrite", "Rewrite trigger", false);
0078   addStringProperty ("rewrite-var", "Rewrite variable");
0079   addStringProperty ("rewrite-text", "Rewrite new text");
0080 
0081   addBoolProperty ("gag", "Gag trigger", false);
0082   addBoolProperty ("notify", "Notify trigger", false);
0083   addBoolProperty ("prompt", "Prompt detection trigger", false);
0084   addBoolProperty ("sound", "Sound trigger", false);
0085   addStringProperty ("sound-file", "Sound file name");
0086 
0087   addBoolProperty ("output-window", "Send output to separate window", false);
0088   addBoolProperty ("output-gag-in-main", "Gag main window in favour of output", false);
0089   addStringProperty ("output-window-name", "Output window name");
0090   
0091   d = new Private;
0092 
0093   d->detectingPrompt = false;
0094   d->curline = nullptr;
0095 }
0096 
0097 cTriggerList::~cTriggerList ()
0098 {
0099   delete d;
0100 }
0101 
0102 cListObject *cTriggerList::newObject ()
0103 {
0104   return new cTrigger (this);
0105 }
0106 
0107 cListEditor *cTriggerList::editor (QWidget *parent)
0108 {
0109   return new cTriggerEditor (parent);
0110 }
0111 
0112 void cTriggerList::matchString (cTextChunk *line)
0113 {
0114   d->curline = line;
0115   d->plainText = d->curline->plainText();
0116 
0117   traverse (TRIGGER_MATCH);
0118 
0119   d->curline = nullptr;
0120 }
0121 
0122 bool cTriggerList::detectingPrompt ()
0123 {
0124   return d->detectingPrompt;
0125 }
0126 
0127 cTextChunk *cTriggerList::lineToMatch ()
0128 {
0129   return d->curline;
0130 }
0131 
0132 QString cTriggerList::stringToMatch ()
0133 {
0134   return d->plainText;
0135 }
0136 
0137 
0138 void cTriggerList::setDetectingPrompt (bool val)
0139 {
0140   d->detectingPrompt = val;
0141 }
0142 
0143 void cTriggerList::processCommands (const QStringList &commands)
0144 {
0145   if (commands.isEmpty())  //do nothing if there are no commands
0146     return;
0147 
0148   //okay, send the commands!
0149   int sess = session();
0150   cCmdQueues *queues = (cCmdQueues *) cActionManager::self()->object ("cmdqueues", sess);
0151   if (!queues) return;
0152   cCmdQueue *queue = new cCmdQueue (sess);
0153   QStringList::const_iterator it;
0154   for (it = commands.begin(); it != commands.end(); ++it)
0155     queue->addCommand (*it);
0156   queues->addQueue (queue);
0157 }
0158 
0159 void cTriggerList::rewriteText (int pos, int len, const QString &newtext)
0160 {
0161   //perform the actual replace
0162   d->curline->replace (pos, len, newtext);
0163   
0164   //plainText has been changed - regenerate it
0165   d->plainText = d->curline->plainText();
0166 }
0167