File indexing completed on 2024-02-18 04:04:41

0001 /***************************************************************************
0002                           ctrigger.cpp  -  trigger
0003     This file is a part of KMuddy distribution.
0004                              -------------------
0005     begin                : Ne okt 13 2002
0006     copyright            : (C) 2002-2008 by Tomas Mecir
0007     email                : kmuddy@kmuddy.com
0008  ***************************************************************************/
0009 
0010 /***************************************************************************
0011  *                                                                         *
0012  *   This program is free software; you can redistribute it and/or modify  *
0013  *   it under the terms of the GNU General Public License as published by  *
0014  *   the Free Software Foundation; either version 2 of the License, or     *
0015  *   (at your option) any later version.                                   *
0016  *                                                                         *
0017  ***************************************************************************/
0018 
0019 #include "ctrigger.h"
0020 
0021 #include <kconfig.h>
0022 #include <kconfiggroup.h>
0023 
0024 cTrigger::cTrigger (int _sess) : sess(_sess)
0025 {
0026   //default compare type is substring
0027   setType (substring);
0028 
0029   continueifmatch = false;
0030   continueifnomatch = true;
0031   dontsend = false;
0032 
0033   colortrigger = false;
0034   clearColorizations ();
0035 
0036   gagtrigger = false;
0037   notifytrigger = false;
0038   prompttrigger = false;
0039   rewritetrigger = false;
0040   soundtrigger = false;
0041 
0042   outputwindowtrigger = false;
0043   gagoutputwindow = false;
0044 }
0045 
0046 cTrigger::~cTrigger ()
0047 {
0048   //nothing here
0049 }
0050 
0051 cSaveableField *cTrigger::newInstance ()
0052 {
0053   return new cTrigger (sess);
0054 }
0055 
0056 void cTrigger::load (KConfig *config, const QString &group)
0057 {
0058   KConfigGroup g = config->group (group);
0059   setText (g.readEntry ("Text", ""));
0060 
0061   //ntxt: for compatibility with KMuddy <= 0.6pre1
0062   QString ntxt = g.readEntry ("Replacement text", QString());
0063 
0064   newtext.clear();
0065   int repcount = g.readEntry ("Replacement count", -1);
0066   if (repcount == -1)   //use old-style replacement
0067     newtext.push_back (ntxt);
0068   else    //new-style replacement
0069     for (int i = 1; i <= repcount; i++)
0070     {
0071       QString repline = g.readEntry ("Replacement line " +
0072             QString::number (i), "");
0073       newtext.push_back (repline);
0074     }
0075 
0076   setType (g.readEntry ("Type", (int)substring));
0077   continueifmatch = g.readEntry ("Continue if match", false);
0078   continueifnomatch = g.readEntry ("Continue if no match", true);
0079   dontsend = g.readEntry ("Show dont send", false);
0080   setCaseSensitive (g.readEntry ("Case sensitive", true));
0081   globalmatch = g.readEntry ("Global matching", false);
0082   setCond (g.readEntry ("Condition", QString()));
0083 
0084   clearColorizations ();
0085   colortrigger = g.readEntry ("Color trigger", false);
0086   if (colortrigger)
0087   {
0088     int cnum = g.readEntry ("Colorizations", 0);
0089     //ensure that cnum is correct
0090     if (cnum > MAX_COLORIZATIONS)
0091       cnum = MAX_COLORIZATIONS;
0092     if (cnum < 0)
0093       cnum = 0;
0094     for (int i = 0; i < cnum; i++)
0095     {
0096       QString cname = g.readEntry ("Colorization variable " +
0097             QString::number(i + 1), "");
0098       int colors = g.readEntry ("Colorization colors " +
0099             QString::number(i + 1), 255 * 256 + 255);
0100       QColor fgc = g.readEntry ("Colorization fg " +
0101             QString::number(i + 1), (QColor) Qt::black);
0102       QColor bgc = g.readEntry ("Colorization bg " +
0103             QString::number(i + 1), (QColor) Qt::black);
0104       addColorization (cname, colors, fgc, bgc);
0105     }
0106   }
0107 
0108   rewritetrigger = g.readEntry ("Rewrite trigger", false);
0109   rewritevar = g.readEntry ("Rewrite variable", QString());
0110   rewritetext = g.readEntry ("Rewrite new text", QString());
0111   
0112   gagtrigger = g.readEntry ("Gag trigger", false);
0113   notifytrigger = g.readEntry ("Notify trigger", false);
0114   prompttrigger = g.readEntry ("Prompt trigger", false);
0115   soundtrigger = g.readEntry ("Sound trigger", false);
0116   soundFName = g.readEntry ("Sound file name", QString());
0117 
0118   outputwindowtrigger = g.readEntry ("Send output to separate window", false);
0119   gagoutputwindow = g.readEntry ("Gag main window in favour of output", false);
0120   outputwindowname = g.readEntry ("Output window name", QString());
0121 }
0122 
0123 void cTrigger::clearColorizations ()
0124 {
0125   colorizationCount = 0;
0126   for (int i = 0; i < MAX_COLORIZATIONS; i++)
0127   {
0128     colorizationVar[i] = QString();
0129     colorization[i] = 255 * 256 + 255;
0130     fgcol[i] = Qt::black;
0131     bgcol[i] = Qt::black;
0132   }
0133 }
0134 
0135 bool cTrigger::addColorization (QString pseudovar, int newcolors, QColor fgc, QColor bgc)
0136 {
0137   if (colorizationCount >= MAX_COLORIZATIONS)
0138     return false;   //NO MORE FREE SPACE!!!
0139   colorizationVar[colorizationCount] = pseudovar;
0140   colorization[colorizationCount] = newcolors;
0141   fgcol[colorizationCount] = fgc;
0142   bgcol[colorizationCount] = bgc;
0143   
0144   colorizationCount++;
0145   return true;
0146 }
0147 
0148 QString cTrigger::getColorizationVariable (int num)
0149 {
0150   if ((num < 0) || (num >= MAX_COLORIZATIONS))
0151     return QString();
0152   return colorizationVar[num];
0153 }
0154 
0155 int cTrigger::getColorizationColor (int num)
0156 {
0157   if ((num < 0) || (num >= MAX_COLORIZATIONS))
0158     return 255 * 256 + 255;
0159   return colorization[num];
0160 }
0161 
0162 QColor cTrigger::getColorizationFg (int num)
0163 {
0164   if ((num < 0) || (num >= MAX_COLORIZATIONS))
0165     return Qt::black;
0166   return fgcol[num];
0167 }
0168 
0169 QColor cTrigger::getColorizationBg (int num)
0170 {
0171   if ((num < 0) || (num >= MAX_COLORIZATIONS))
0172     return Qt::black;
0173   return bgcol[num];
0174 }
0175 
0176