summaryrefslogtreecommitdiffstats
path: root/src/Client/UserListCommands.cpp
blob: d60257908302dcf99d8a84e766cc95ea696a6143 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
 * UserListCommands.cpp
 *
 * Copyright (C) 2009 Matthias Schiffer <matthias@gamezock.de>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "UserListCommands.h"
#include "Application.h"
#include "XLSReader.h"
#include "XLSSheet.h"
#include "Requests/UserLists/UserListListRequest.h"
#include "Requests/UserLists/UserListDownloadRequest.h"
#include "Requests/UserLists/UserListDiffListRequest.h"

#include <Common/RequestManager.h>
#include <Common/UserLists/UserList.h>
#include <Common/UserLists/Util.h>

#include <iostream>

namespace Mad {
namespace Client {

const UserListCommands::Command UserListCommands::commands[] = {
    {{"help", "?", 0}, "help [command]", "Display usage information about user list commands", "Display usage information about a user list command. If no command is given, display a list of all available user list commands.", &UserListCommands::helpCommand},
    {{"list", "ls", 0}, "list", "List the stored user lists", "List the stored user lists.", &UserListCommands::listCommand},
    {{"show_list", "sl", 0}, "show_list list", "Displays a user list", "Displays a user list.", &UserListCommands::showListCommand},
    {{"import", "im", 0}, "import filename", "Imports a user list file", "Imports a user list file. At the moment, this supports only XLS files.", &UserListCommands::importCommand},
    {{0}, 0, 0, 0, 0}
};

const UserListCommands::Command* UserListCommands::findCommand(const Core::String& command) {
  for(int i = 0; commands[i].commands[0] != 0; ++i) {
    for(int j = 0; commands[i].commands[j] != 0; ++j) {
      if(command == commands[i].commands[j]) {
        return &commands[i];
      }
    }
  }

  return 0;
}

void UserListCommands::printUsage(const Core::String& command) {
  const UserListCommands::Command *cmd = findCommand(command);

  if(cmd)
    std::cerr << "Usage: " << "user_lists " << cmd->cmdline << std::endl;
}


void UserListCommands::helpCommand(CommandParser* /*commandParser*/, const std::vector<Core::String> &args) {
  if(args.size() <= 2) {
    std::cout << "Available commands:" << std::endl << std::endl;

    for(int i = 0; commands[i].commands[0] != 0; ++i) {
      std::cout << commands[i].commands[0];

      for(int j = 1; commands[i].commands[j] != 0; ++j)
        std::cout << ", " << commands[i].commands[j];

      std::cout << std::endl << "\t" << commands[i].desc << std::endl;
    }
  }
  else if(args.size() == 3) {
    const Command* command = findCommand(args[2]);

    if(command) {
      std::cout << "Usage: " << "user_lists " << command->cmdline << std::endl << std::endl;
      std::cout << command->longdesc << std::endl << std::endl;
    }
    else
      std::cerr << args[0] << " " << args[1] << ": Command '" << args[2] << "' doesn't exist." << std::endl;
  }
  else {
    std::cerr << args[0] << " " << args[1] << ": Too many arguments." << std::endl;
    printUsage("help");
  }
}

void UserListCommands::listCommand(CommandParser *commandParser, const std::vector<Core::String>& /*args*/) {
  boost::shared_ptr<Requests::UserLists::UserListListRequest> userListRequest(new Requests::UserLists::UserListListRequest(commandParser->application));
  boost::shared_ptr<Requests::UserLists::UserListDiffListRequest> userListDiffRequest(new Requests::UserLists::UserListDiffListRequest(commandParser->application));

  commandParser->application->getRequestManager()->sendRequest(commandParser->connection, userListRequest);
  commandParser->application->getRequestManager()->sendRequest(commandParser->connection, userListDiffRequest);

  userListRequest->wait();
  userListDiffRequest->wait();

  std::pair<boost::shared_ptr<const Common::XmlData>, Core::Exception> userListResult = userListRequest->getResult();
  std::pair<boost::shared_ptr<const Common::XmlData>, Core::Exception> userListDiffResult = userListDiffRequest->getResult();

  if(userListResult.second) {
    std::cerr << "An error occurred during your request: " << userListResult.second.what() << "." << std::endl;
    return;
  }
  else if(!userListResult.first || !userListDiffResult.first || userListDiffResult.second) {
    std::cerr << "An error occurred during your request: " << userListDiffResult.second.what() << "." << std::endl;
    return;
  }

  const Common::XmlData::List *userLists = userListResult.first->getList("userLists");
  const Common::XmlData::List *userListDiffs = userListDiffResult.first->getList("userListDiffs");

  if(userLists->isEmpty()) {
    std::cout << "There aren't any user lists stored on the server." << std::endl;
  }
  else {
    if(userLists->getSize() == 1)
      std::cout << "There is 1 user list stored on the server:" << std::endl;
    else
      std::cout << "There are " << userLists->getSize() << " user lists stored on the server:" << std::endl;

    for(Common::XmlData::List::const_iterator userList = userLists->begin(); userList != userLists->end(); ++userList) {
      std::cout << "  " << userList->get<const Core::String&>("name") << std::endl;
    }
  }

  std::cout << std::endl;

  if(!userListDiffs->isEmpty()) {
    if(userListDiffs->getSize() == 1)
      std::cout << "There is 1 user list difference record stored on the server:" << std::endl;
    else
      std::cout << "There are " << userListDiffs->getSize() << " user list difference records stored on the server:" << std::endl;

    for(Common::XmlData::List::const_iterator diff = userListDiffs->begin(); diff != userListDiffs->end(); ++diff) {
      std::cout << "  " << diff->get<const Core::String&>("name") << std::endl;
    }
  }
}

void UserListCommands::showListCommand(CommandParser *commandParser, const std::vector<Core::String> &args) {
  if(args.size() < 3) {
    std::cerr << args[0] << " " << args[1] << ": No list name given." << std::endl;
    printUsage("show_list");
    return;
  }
  else if(args.size() == 3) {
    boost::shared_ptr<Requests::UserLists::UserListDownloadRequest> request(
        new Requests::UserLists::UserListDownloadRequest(commandParser->application, args[2]));
    commandParser->application->getRequestManager()->sendRequest(commandParser->connection, request);
    request->wait();

    std::pair<boost::shared_ptr<const Common::XmlData>, Core::Exception> result = request->getResult();

    if(!result.first || result.second) {
      std::cerr << "An error occurred during your request: " << result.second.what() << "." << std::endl;
      return;
    }

    boost::shared_ptr<Common::UserLists::UserList> userList = Common::UserLists::Util::deserializeUserList(result.first.get());

    std::cout << "User list '" << args[2];

    if(userList->isEmpty()) {
      std::cout << " is empty." << std::endl;
    }
    else {
      std::cout << "' contains " << userList->getLength() << " user";
      if(userList->getLength() > 1)
        std::cout << "s";
      std::cout << "." << std::endl << std::endl;

      printUserList(*userList);
    }

    std::cout << std::endl;
  }
  else {
    std::cerr << args[0] << " " << args[1] << ": Too many arguments." << std::endl;
    printUsage("show_list");
    return;
  }
}

void UserListCommands::importCommand(CommandParser* /*commandParser*/, const std::vector<Core::String> &args) {
  if(args.size() < 3) {
    std::cerr << args[0] << " " << args[1] << ": No filename given." << std::endl;
    printUsage("import");
    return;
  }
  else if(args.size() == 3) {
    try {
      XLSReader reader(args[2].toLocale());
      const std::list<boost::shared_ptr<XLSSheet> > &sheets = reader.getSheets();

      if(sheets.empty()) {
        std::cerr << "There aren't any worksheets in the file." << std::endl;
        return;
      }

      unsigned long sheetNum = 1;

      if(sheets.size() > 1) {
        std::cout << "There is more than one worksheet in the file. Which one do you want to import?" << std::endl << std::endl;

        while(true) {
          int i = 1;
          for(std::list<boost::shared_ptr<XLSSheet> >::const_iterator sheet = sheets.begin(); sheet != sheets.end(); ++sheet, ++i) {
            std::cout << "  " << i << ") " << (*sheet)->getTitle() << std::endl;
          }

          std::cout << std::endl;
          std::cout << "Worksheet number: " << std::flush;

          std::string input;
          std::getline(std::cin, input);
          sheetNum = std::strtoul(input.c_str(), 0, 10);

          if(sheetNum > 0 && sheetNum <= sheets.size())
            break;

          std::cout << "Invalid selection. Please choose one of:" << std::endl << std::endl;
        }
      }

      std::list<boost::shared_ptr<XLSSheet> >::const_iterator sheet = sheets.begin();
      std::advance(sheet, sheetNum-1);

      std::cout << "Imported worksheet:" << std::endl;
      printSheet(**sheet, 10);

      std::cout << std::endl << std::endl;
      std::cout << "Does the first line of the worksheet contain the column names? (Y/n) " << std::flush;

      std::string input;
      std::getline(std::cin, input);
      if(input.empty() || (input[0] != 'n' && input[0] != 'N')) {
        (*sheet)->useFirstRowAsColumnNames(true);
      }

      std::cout << std::endl << std ::endl;
      printSheet(**sheet, 10);
      std::cout << std::endl << std ::endl;

      Common::UserLists::UserList userList;
      const std::vector<XLSSheet::RowType> &rows = (*sheet)->getRows();
      unsigned id = 1;

      for(std::vector<XLSSheet::RowType>::const_iterator rowIt = rows.begin(); rowIt != rows.end(); ++rowIt, ++id) {
        const std::vector<Core::String> &row = **rowIt;
        Common::UserLists::UserListEntry entry;

        std::ostringstream stream;
        stream << id;
        entry.setName(stream.str().c_str());

        for(unsigned col = 0; col < (*sheet)->getColumnCount(); ++col) {
          entry.setDetail((*sheet)->getColumnName(col), row[col]);
        }

        userList.addUser(entry);
      }

      printUserList(userList);
      std::cout << std::endl << std ::endl;
    }
    catch(Core::Exception e) {
      std::cerr << "The file can't be read: " << e.what() << "." << std::endl;
    }
  }
  else {
    std::cerr << args[0] << " " << args[1] << ": Too many arguments." << std::endl;
    printUsage("import");
    return;
  }
}


void UserListCommands::printUserList(const Common::UserLists::UserList &list) {
  std::map<Core::String, boost::int32_t> lengths;

  static const Core::String USER_NAME = "User name";
  static const Core::String GROUP_NAME = "Group name";

  lengths.insert(std::make_pair(USER_NAME, USER_NAME.length()));
  lengths.insert(std::make_pair(GROUP_NAME, GROUP_NAME.length()));

  std::set<Core::String> details = list.getDetails();
  for(std::set<Core::String>::iterator detail = details.begin(); detail != details.end(); ++detail) {
    lengths.insert(std::make_pair(*detail, detail->length()));
  }

  for(Common::UserLists::UserList::const_iterator user = list.begin(); user != list.end(); ++user) {
    std::map<Core::String, boost::int32_t>::iterator it = lengths.find(USER_NAME);
    if(user->getName().length() > it->second)
      it->second = user->getName().length();

    it = lengths.find(GROUP_NAME);
    if(user->getGroup().length() > it->second)
      it->second = user->getGroup().length();

    for(std::set<Core::String>::iterator detail = details.begin(); detail != details.end(); ++detail) {
      it = lengths.find(*detail);
      Core::String detailStr = user->getDetail(*detail);
      if(detailStr.length() > it->second)
        it->second = detailStr.length();
    }
  }

  std::cout << " ";
  printPadded(USER_NAME, lengths.find(USER_NAME)->second);
  std::cout << "   ";
  printPadded(GROUP_NAME, lengths.find(GROUP_NAME)->second);
  for(std::set<Core::String>::iterator detail = details.begin(); detail != details.end(); ++detail) {
    std::cout << "   ";
    printPadded(*detail, lengths.find(*detail)->second);
  }
  std::cout << std::endl;

  std::cout << "-";
  printDelimiter(lengths.find(USER_NAME)->second);
  std::cout << "- -";
  printDelimiter(lengths.find(GROUP_NAME)->second);
  for(std::set<Core::String>::iterator detail = details.begin(); detail != details.end(); ++detail) {
    std::cout << "- -";
    printDelimiter(lengths.find(*detail)->second);
  }
  std::cout << "-" << std::endl;

  for(Common::UserLists::UserList::const_iterator user = list.begin(); user != list.end(); ++user) {
    static const Core::String UNSET("<unset>");

    std::cout << " ";

    if(!user->getName().isEmpty())
      printPadded(user->getName(), lengths.find(USER_NAME)->second);
    else
      printPadded(UNSET, lengths.find(USER_NAME)->second);

    std::cout << "   ";

    if(!user->getGroup().isEmpty())
      printPadded(user->getGroup(), lengths.find(GROUP_NAME)->second);
    else
      printPadded(UNSET, lengths.find(GROUP_NAME)->second);

    for(std::set<Core::String>::iterator detail = details.begin(); detail != details.end(); ++detail) {
      std::cout << "   ";
      printPadded(user->getDetail(*detail), lengths.find(*detail)->second);
    }

    std::cout << std::endl;
  }
}

void UserListCommands::printSheet(const XLSSheet &sheet, unsigned rowCount) {
  const std::vector<XLSSheet::RowType> &rows = sheet.getRows();
  std::vector<boost::int32_t> lengths(sheet.getColumnCount());

  for(unsigned col = 0; col < sheet.getColumnCount(); ++col) {
    lengths[col] = sheet.getColumnName(col).length();
  }

  for(std::vector<XLSSheet::RowType>::const_iterator rowIt = rows.begin(); rowIt != rows.end() && (rowCount == 0 || rowIt != rows.begin()+rowCount); ++rowIt) {
    const std::vector<Core::String> &row = **rowIt;

    for(unsigned col = 0; col < sheet.getColumnCount(); ++col) {
      if(row[col].length() > lengths[col])
        lengths[col] = row[col].length();
    }
  }

  for(unsigned col = 0; col < sheet.getColumnCount(); ++col) {
    std::cout << "   ";
    printPadded(sheet.getColumnName(col), lengths[col]);
  }
  std::cout << std::endl;

  std::cout << " ";
  for(unsigned col = 0; col < sheet.getColumnCount(); ++col) {
    std::cout << " -";
    printDelimiter(lengths[col]);
    std::cout << "-";
  }
  std::cout << std::endl;

  for(std::vector<XLSSheet::RowType>::const_iterator rowIt = rows.begin(); rowIt != rows.end() && (rowCount == 0 || rowIt != rows.begin()+rowCount); ++rowIt) {
    const std::vector<Core::String> &row = **rowIt;

    for(unsigned col = 0; col < sheet.getColumnCount(); ++col) {
      std::cout << "   ";
      printPadded(row[col], lengths[col]);
    }

    std::cout << std::endl;
  }

  if(rowCount > 0 && rows.size() > rowCount) {
    for(unsigned col = 0; col < sheet.getColumnCount(); ++col) {
      std::cout << "   ";
      printPadded("...", lengths[col]);
    }
  }
}

void UserListCommands::printPadded(const Core::String &str, boost::int32_t length) {
  std::cout << str;
  if(str.length() < length)
    std::cout << std::string(length-str.length(), ' ');
}

void UserListCommands::printDelimiter(boost::int32_t length) {
  printPadded(Core::String(std::string(length, '-').c_str()), length);
}


void UserListCommands::userListCommand(CommandParser *commandParser, const std::vector<Core::String> &args) {
  Core::String cmd;

  if(args.size() < 2)
    cmd = "help";
  else
    cmd = args[1];

  const Command* command = findCommand(cmd);

  if(command)
    command->function(commandParser, args);
  else
    std::cerr << args[0] << ": Unknown command '" << cmd << "'." << std::endl;
}

}
}