OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / tests / manual / ssh / sftp / argumentscollector.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "argumentscollector.h"
34
35 #include <iostream>
36
37 using namespace std;
38 using namespace Utils;
39
40 ArgumentsCollector::ArgumentsCollector(const QStringList &args)
41     : m_arguments(args)
42 {
43 }
44
45 Parameters ArgumentsCollector::collect(bool &success) const
46 {
47     Parameters parameters;
48     try {
49         bool authTypeGiven = false;
50         bool portGiven = false;
51         bool timeoutGiven = false;
52         bool smallFileCountGiven = false;
53         bool bigFileSizeGiven = false;
54         bool proxySettingGiven = false;
55         int pos;
56         int port;
57         for (pos = 1; pos < m_arguments.count() - 1; ++pos) {
58             if (checkAndSetStringArg(pos, parameters.sshParams.host, "-h")
59                 || checkAndSetStringArg(pos, parameters.sshParams.userName, "-u"))
60                 continue;
61             if (checkAndSetIntArg(pos, port, portGiven, "-p")
62                 || checkAndSetIntArg(pos, parameters.sshParams.timeout, timeoutGiven, "-t")
63                 || checkAndSetIntArg(pos, parameters.smallFileCount, smallFileCountGiven, "-c")
64                 || checkAndSetIntArg(pos, parameters.bigFileSize, bigFileSizeGiven, "-s"))
65                 continue;
66             if (checkAndSetStringArg(pos, parameters.sshParams.password, "-pwd")) {
67                 if (!parameters.sshParams.privateKeyFile.isEmpty())
68                     throw ArgumentErrorException(QLatin1String("-pwd and -k are mutually exclusive."));
69                 parameters.sshParams.authenticationType
70                     = SshConnectionParameters::AuthenticationByPassword;
71                 authTypeGiven = true;
72                 continue;
73             }
74             if (checkAndSetStringArg(pos, parameters.sshParams.privateKeyFile, "-k")) {
75                 if (!parameters.sshParams.password.isEmpty())
76                     throw ArgumentErrorException(QLatin1String("-pwd and -k are mutually exclusive."));
77                 parameters.sshParams.authenticationType
78                     = SshConnectionParameters::AuthenticationByKey;
79                 authTypeGiven = true;
80                 continue;
81             }
82             if (!checkForNoProxy(pos, parameters.sshParams.proxyType, proxySettingGiven))
83                 throw ArgumentErrorException(QLatin1String("unknown option ") + m_arguments.at(pos));
84         }
85
86         Q_ASSERT(pos <= m_arguments.count());
87         if (pos == m_arguments.count() - 1) {
88             if (!checkForNoProxy(pos, parameters.sshParams.proxyType, proxySettingGiven))
89                 throw ArgumentErrorException(QLatin1String("unknown option ") + m_arguments.at(pos));
90         }
91
92         if (!authTypeGiven)
93             throw ArgumentErrorException(QLatin1String("No authentication argument given."));
94         if (parameters.sshParams.host.isEmpty())
95             throw ArgumentErrorException(QLatin1String("No host given."));
96         if (parameters.sshParams.userName.isEmpty())
97             throw ArgumentErrorException(QLatin1String("No user name given."));
98
99         parameters.sshParams.port = portGiven ? port : 22;
100         if (!timeoutGiven)
101             parameters.sshParams.timeout = 30;
102         if (!smallFileCountGiven)
103             parameters.smallFileCount = 1000;
104         if (!bigFileSizeGiven)
105             parameters.bigFileSize = 1024;
106         success = true;
107     } catch (ArgumentErrorException &ex) {
108         cerr << "Error: " << qPrintable(ex.error) << endl;
109         printUsage();
110         success = false;
111     }
112     return parameters;
113 }
114
115 void ArgumentsCollector::printUsage() const
116 {
117     cerr << "Usage: " << qPrintable(m_arguments.first())
118         << " -h <host> -u <user> "
119         << "-pwd <password> | -k <private key file> [ -p <port> ] "
120         << "[ -t <timeout> ] [ -c <small file count> ] "
121         << "[ -s <big file size in MB> ] [ -no-proxy ]" << endl;
122 }
123
124 bool ArgumentsCollector::checkAndSetStringArg(int &pos, QString &arg, const char *opt) const
125 {
126     if (m_arguments.at(pos) == QLatin1String(opt)) {
127         if (!arg.isEmpty()) {
128             throw ArgumentErrorException(QLatin1String("option ") + opt
129                 + QLatin1String(" was given twice."));
130         }
131         arg = m_arguments.at(++pos);
132         if (arg.isEmpty() && QLatin1String(opt) != QLatin1String("-pwd"))
133             throw ArgumentErrorException(QLatin1String("empty argument not allowed here."));
134         return true;
135     }
136     return false;
137 }
138
139 bool ArgumentsCollector::checkAndSetIntArg(int &pos, int &val,
140     bool &alreadyGiven, const char *opt) const
141 {
142     if (m_arguments.at(pos) == QLatin1String(opt)) {
143         if (alreadyGiven) {
144             throw ArgumentErrorException(QLatin1String("option ") + opt
145                 + QLatin1String(" was given twice."));
146         }
147         bool isNumber;
148         val = m_arguments.at(++pos).toInt(&isNumber);
149         if (!isNumber) {
150             throw ArgumentErrorException(QLatin1String("option ") + opt
151                  + QLatin1String(" needs integer argument"));
152         }
153         alreadyGiven = true;
154         return true;
155     }
156     return false;
157 }
158
159 bool ArgumentsCollector::checkForNoProxy(int &pos,
160     SshConnectionParameters::ProxyType &type, bool &alreadyGiven) const
161 {
162     if (m_arguments.at(pos) == QLatin1String("-no-proxy")) {
163         if (alreadyGiven)
164             throw ArgumentErrorException(QLatin1String("proxy setting given twice."));
165         type = SshConnectionParameters::NoProxy;
166         alreadyGiven = true;
167         return true;
168     }
169     return false;
170 }