Advertisement
agrippa1994

QChatlog

Sep 24th, 2012
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ifndef CHATLOG_H
  2. #define CHATLOG_H
  3.  
  4. #include <QThread>
  5. #include <QFile>
  6. #include <QStringList>
  7. #include <QFile>
  8. #include <QRegExp>
  9. #include <QSettings>
  10.  
  11. class QChatlog : public QThread
  12. {
  13.     Q_OBJECT
  14. public:
  15.     explicit QChatlog(QObject *parent = 0);
  16.     ~QChatlog();
  17.     bool KillThread();
  18.     void GetLines();
  19. signals:
  20.     void OnMedicHeal(QString Medic,int Price);
  21.     void OnHotdog(QString Seller,int Price);
  22.     void OnLineRead(QString Line);
  23. protected:
  24.     void run();
  25. private:
  26.     void DecomposeLine(QString Line);
  27.     bool BreakThread;
  28.     int LineCount;
  29.     QStringList ChatlogLines;
  30.     QFile * Chatlog;
  31.  
  32.  
  33. };
  34.  
  35. #endif // CHATLOG_H
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44. #include "Chatlog.h"
  45.  
  46. QChatlog::QChatlog(QObject *parent) : QThread(parent), BreakThread(false), LineCount(0)
  47. {
  48.     QSettings settings(QSettings::UserScope, "Microsoft", "Windows");
  49.     settings.beginGroup("CurrentVersion/Explorer/Shell Folders");
  50.     QString Path = settings.value("Personal").toString();
  51.     Path.append("\\GTA San Andreas User Files\\SAMP\\chatlog.txt");
  52.  
  53.     Chatlog=new QFile(Path);
  54.     ChatlogLines.clear();
  55.     Chatlog->open(QIODevice::ReadOnly | QIODevice::Text);
  56.     GetLines();
  57.     start();
  58. }
  59.  
  60. QChatlog::~QChatlog()
  61. {
  62.     KillThread();
  63.     ChatlogLines.clear();
  64.     Chatlog->close();
  65.     delete Chatlog;
  66. }
  67.  
  68. void QChatlog::run()
  69. {
  70.     while(!BreakThread)
  71.     {
  72.         this->msleep(500);
  73.         int OldLineCount=LineCount;
  74.         GetLines();
  75.         for(int i=OldLineCount;i<LineCount;i++)
  76.             DecomposeLine(this->ChatlogLines.at(i));
  77.     }
  78.     return;
  79. }
  80. void QChatlog::GetLines()
  81. {
  82.     while(!Chatlog->atEnd())
  83.     {
  84.         LineCount++;
  85.         ChatlogLines.append(Chatlog->readLine());
  86.     }
  87. }
  88.  
  89. bool QChatlog::KillThread()
  90. {
  91.     if(!isRunning()) return false;
  92.     BreakThread=true;
  93.     terminate();
  94.     wait();
  95.     BreakThread=false;
  96.     return isFinished();
  97. }
  98.  
  99. void QChatlog::DecomposeLine(QString Line)
  100. {
  101.     if(Line.indexOf("bietet dir eine Heilung für")!=-1)
  102.     {
  103.         QRegExp r("(.*)Arzt (\\w+)(.*)\\$(\\d+)(.*)");
  104.         if(r.indexIn(Line)!=-1)
  105.             emit OnMedicHeal(r.cap(2),r.cap(4).toInt());
  106.     }
  107.     else if(Line.indexOf("bietet dir einen Hotdog für")!=-1)
  108.     {
  109.         QRegExp r("(.*)Verkäufer (\\w+)(.*)\\$(\\d+)(.*)");
  110.         if(r.indexIn(Line)!=-1)
  111.             emit OnHotdog(r.cap(2),r.cap(4).toInt());
  112.     }
  113.     else emit OnLineRead(Line);
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement