| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2006-2008 fullmetalcoder <[email protected]> |
| 4 | ** |
| 5 | ** This file is part of the Edyuk project <http://edyuk.org> |
| 6 | ** |
| 7 | ** This file may be used under the terms of the GNU General Public License |
| 8 | ** version 2 as published by the Free Software Foundation and appearing in the |
| 9 | ** file GPL.txt included in the packaging of this file. |
| 10 | ** |
| 11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
| 12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | ** |
| 14 | ****************************************************************************/ |
| 15 | |
| 16 | #include "qmanagedrequest.h" |
| 17 | |
| 18 | /*! |
| 19 | \file qmanagedrequest.cpp |
| 20 | |
| 21 | \brief Implementation of the QManagedRequest class. |
| 22 | */ |
| 23 | |
| 24 | QManagedRequest::QManagedRequest(const QString& cmd, const QStringList& args) |
| 25 | : sCommand(cmd), lArguments(args) {} |
| 26 | |
| 27 | QManagedRequest::QManagedRequest(const QManagedRequest& r) |
| 28 | : sCommand(r.sCommand), lArguments(r.lArguments) {} |
| 29 | |
| 30 | QManagedRequest::~QManagedRequest() { |
| 31 | sCommand.clear(); |
| 32 | lArguments.clear(); |
| 33 | } |
| 34 | |
| 35 | QManagedRequest QManagedRequest::createSystemRequest(const QStringList& args, |
| 36 | int id) { |
| 37 | QManagedRequest r("--request"); |
| 38 | r.lArguments = args; |
| 39 | r.lArguments.prepend(QString::number(id)); |
| 40 | |
| 41 | return r; |
| 42 | } |
| 43 | |
| 44 | QManagedRequest& QManagedRequest::operator=(const QManagedRequest& r) { |
| 45 | sCommand = r.sCommand; |
| 46 | lArguments = r.lArguments; |
| 47 | |
| 48 | return *this; |
| 49 | } |
| 50 | |
| 51 | QString QManagedRequest::toString() const { |
| 52 | return QString::fromUtf8(joinArguments(sCommand, lArguments)); |
| 53 | } |
| 54 | |
| 55 | QManagedRequest QManagedRequest::fromString(const QString& s) { |
| 56 | QManagedRequest r(""); |
| 57 | r.lArguments = splitArguments(s); |
| 58 | r.sCommand = r.lArguments.takeAt(0); |
| 59 | |
| 60 | return r; |
| 61 | } |
| 62 | |
| 63 | QStringList QManagedRequest::splitArguments(const QString& s) { |
| 64 | int i = -1; |
| 65 | QStringList d, l = s.split(QRegExp("\\s")); |
| 66 | |
| 67 | while (++i < l.count()) { |
| 68 | if (l.at(i).startsWith("\"")) { |
| 69 | QString arg; |
| 70 | |
| 71 | do { |
| 72 | arg += " "; |
| 73 | arg += l.at(i); |
| 74 | } while (!arg.endsWith("\"") && (++i < l.count())); |
| 75 | |
| 76 | arg.remove(0, 2); |
| 77 | |
| 78 | if (arg.endsWith("\"")) arg.chop(1); |
| 79 | |
| 80 | d << arg; |
| 81 | } else { |
| 82 | d << l.at(i); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return d; |
| 87 | } |
| 88 | |
| 89 | QByteArray QManagedRequest::joinArguments(const QString& cmd, |
| 90 | const QStringList& l) { |
| 91 | QByteArray msg; |
| 92 | |
| 93 | msg += cmd.toUtf8(); |
| 94 | |
| 95 | foreach (QString a, l) { |
| 96 | if (a.isEmpty()) continue; |
| 97 | |
| 98 | if (a.contains(QRegExp("\\s"))) a = "\"" + a + "\""; |
| 99 | |
| 100 | msg += " "; |
| 101 | msg += a.toUtf8(); |
| 102 | } |
| 103 | |
| 104 | return msg; |
| 105 | } |