Advertisement
Swiftkill

Task Importer

Nov 2nd, 2016
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.14 KB | None | 0 0
  1. #include <QtCore/QCoreApplication>
  2. #include <QProcess>
  3. #include <iostream>
  4. #include <QFile>
  5. #include <QTemporaryFile>
  6. #include <QTextStream>
  7.  
  8.  
  9.  
  10. bool runProcess( QString file, QString name)
  11. {
  12.     QProcess  *scheduler = new QProcess();
  13.         scheduler->setProcessChannelMode(QProcess::ForwardedChannels);
  14.     QStringList args;
  15.     args << "/Create";
  16.     args << "/XML" << file;
  17.     args << "/TN"  << name;
  18.  
  19.     std::cout << "Creating task: " << name.toStdString() << std::endl;
  20.     scheduler->start("schtasks", args);
  21.     scheduler->waitForFinished();
  22.     return scheduler->exitCode() == 0;
  23. }
  24.  
  25. enum ParseState { eScan, eWrite, eCommit };
  26.  
  27. // Importing tasks from xml (not really) dump made by schtasks
  28. int main(int argc, char *argv[])
  29. {
  30.     QCoreApplication a(argc, argv);
  31.     int         count = 0, total = 0;
  32.  
  33.     // open file
  34.     QString filename;
  35.     std::string fn;
  36.     if(argc < 2)
  37.     {
  38.         std::cout << "Enter name of file, containing schtasks dump: ";
  39.         std::cin >> fn;
  40.         filename= QString::fromStdString(fn);
  41.     }
  42.     else
  43.     {
  44.         filename = argv[1];
  45.     }
  46.     QFile inputFile(filename);
  47.     if (!inputFile.open(QIODevice::ReadOnly | QIODevice::Text))
  48.     {
  49.         std::cout << "Cannot open file: " << inputFile.fileName().toStdString() << std::endl;
  50.         return -1;
  51.     }
  52.     std::cout << "Importing from file: " << inputFile.fileName().toStdString() << std::endl;
  53.  
  54.     // create Temporary file
  55.     QTemporaryFile *tmpFile;
  56.     enum ParseState state = eScan;
  57.  
  58.     QRegExp rxName("<!--([^#]+)-->");
  59.     QString taskName, tmpFileName;
  60.     bool error = false;
  61.     while (!inputFile.atEnd() && !error)
  62.     {
  63.          QByteArray line = inputFile.readLine();
  64.          switch(state)
  65.          {
  66.          case eScan:
  67.              if ( rxName.indexIn(line) != -1)//(line.contains("<!--"))
  68.              {
  69.                  taskName = rxName.cap(1).trimmed();
  70.                  std::cout << "Task: " << taskName.toStdString() << std::endl;
  71.                  tmpFile = new QTemporaryFile;
  72.                  tmpFile->setAutoRemove(false);
  73.                  if(!tmpFile->open())
  74.                  {
  75.                      std::cout << "Cannot create temporary file\n ";
  76.                  }
  77.                  else
  78.                  {
  79.                      state = eWrite;
  80.                  }
  81.              }
  82.              break;
  83.          case eWrite:
  84.              tmpFile->write(line);
  85.              if(line.contains("</Task>"))
  86.              {
  87.                      tmpFile->flush();
  88.                      state = eCommit;
  89.              }
  90.              break;
  91.          case eCommit:
  92.              //tmpFile->close();
  93.              tmpFileName =tmpFile->fileName();
  94.              delete tmpFile;
  95.              count += (runProcess(tmpFileName,taskName))? 1 : 0;
  96.              total++;
  97.              QFile::remove(tmpFileName);
  98.              state = eScan;
  99.              break;
  100.          }
  101.     }
  102.  
  103.     if(!error && (state != eScan))
  104.     {
  105.         std::cout << "Unexpected end of file: " << std::endl;
  106.         return -1;
  107.     }
  108.  
  109.     std::cout << "Tasks created: " <<  count << " out of " << total;
  110.  
  111.     return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement