-
Notifications
You must be signed in to change notification settings - Fork 0
/
doclist_sample.cc
278 lines (241 loc) · 8.57 KB
/
doclist_sample.cc
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
/**
* Copyright 2008 Google Inc. All Rights Reserved.
* Author: [email protected] (Eric Bidelman)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
#include "gdata/util/string_utils.h"
#include "gdata/client/doc_list_service.h"
using namespace std;
using gdata::client::DocListService;
using gdata::util::StringUtils;
enum {LIST_ALL = 1,
LIST_DOCSFOLDERS,
SEARCH_DOCS,
UPLOAD_DOC,
DELETE_DOC,
EXIT};
const int NUM_MAIN_MENU_OPTIONS = 6;
const string MAIN_MENU[NUM_MAIN_MENU_OPTIONS] =
{"List all of your documents",
"List specific documents/folders",
"Search your documents",
"Upload a document",
"Delete a document/folder",
"Exit"};
const int NUM_LIST_DOC_OPTIONS = 9;
const string LIST_DOC_MENU[NUM_LIST_DOC_OPTIONS] =
{"Folders",
"Word documents",
"Spreadsheets",
"Presentations",
"Starred documents",
"Trashed documents\n",
"List folder contents",
"List sharing permissions\n",
"< Back"};
enum {LIST_FOLDERS = 1,
LIST_DOCUMENTS,
LIST_SPREADSHEETS,
LIST_PRESOS,
LIST_STARRED,
LIST_TRASHED,
LIST_DOCS_IN_FOLDER,
LIST_ACLS,
BACK};
void PrintMainMenu();
void PrintListDocSubMenu();
int GetMenuChoice(int num_options);
int main(int argc, char *argv[]) {
string email, password;
if (argc != 3) {
cout << "Email address: ";
cin >> email;
cout << "password: ";
cin >> password;
} else {
email = argv[1];
password = argv[2];
}
cout << setfill('=') << setw(60) << "=" << endl
<< "Documents List API Sample" << endl
<< setfill('=') << setw(60) << "=" << endl
<< "Authenticating " << email << "...";
DocListService service(email, password, "DocListCppSample-v1");
cout << "OK!" << endl;
while (1) {
string url, q, doc_title, doc_path;
int index, sub_menu_choice;
vector< map<string, string> > doc_listing;
string doc_list_feed = DocListService::kDocListScope +
DocListService::kDocListFeed;
PrintMainMenu();
switch (GetMenuChoice(NUM_MAIN_MENU_OPTIONS)) {
case LIST_ALL:
url = doc_list_feed;
cout << "Retrieving all documents: " << url << endl << endl;
doc_listing = service.ListDocuments(url);
break;
case LIST_DOCSFOLDERS:
do {
PrintListDocSubMenu();
sub_menu_choice = GetMenuChoice(NUM_LIST_DOC_OPTIONS);
switch (sub_menu_choice) {
case LIST_FOLDERS:
url = doc_list_feed + DocListService::kFolderCategory +
"?showfolders=true";
cout << "Retrieving list of folders: " << url << endl << endl;
doc_listing = service.ListDocuments(url);
break;
case LIST_DOCUMENTS:
url = doc_list_feed + DocListService::kDocumentCategory;
cout << "Retrieving word docuemnts: " << url << endl << endl;
doc_listing = service.ListDocuments(url);
break;
case LIST_SPREADSHEETS:
url = doc_list_feed + DocListService::kSpreadsheetCategory;
cout << "Retrieving spreadsheets: " << url << endl << endl;
doc_listing = service.ListDocuments(url);
break;
case LIST_PRESOS:
url = doc_list_feed + DocListService::kPresentationCategory;
cout << "Retrieving presentations: " << url << endl << endl;
doc_listing = service.ListDocuments(url);
break;
case LIST_STARRED:
url = doc_list_feed + DocListService::kStarredCategory;
cout << "Retrieving starred documents: " << url << endl << endl;
doc_listing = service.ListDocuments(url);
break;
case LIST_TRASHED:
url = doc_list_feed + DocListService::kTrashedCategory;
cout << "Retrieving list of trashed docs: " << url << "\n\n";
doc_listing = service.ListDocuments(url);
break;
case LIST_DOCS_IN_FOLDER:
url = doc_list_feed + DocListService::kFolderCategory +
"?showfolders=true";
doc_listing = service.ListDocuments(url, false);
for (unsigned int row = 0; row < doc_listing.size(); ++row) {
cout << row + 1 << ". [" << doc_listing[row]["doc_type"]
<< "] " << doc_listing[row]["title"] << endl;
}
cout << "\nWhich folder? ";
index = GetMenuChoice(doc_listing.size()) - 1;
url = doc_listing[index]["content_src"];
cout << "Retrieving contents of folder '"
<< doc_listing[index]["title"] << "': " << url << "\n\n";
doc_listing = service.ListDocuments(url);
break;
case LIST_ACLS:
url = doc_list_feed;
doc_listing = service.ListDocuments(url, false);
for (unsigned int row = 0; row < doc_listing.size(); ++row) {
cout << row + 1 << ". [" << doc_listing[row]["doc_type"]
<< "] " << doc_listing[row]["title"] << endl;
}
cout << "\nWhich document? ";
index = GetMenuChoice(doc_listing.size()) - 1;
url = doc_listing[index]["acl_feedLink"];
cout << "Retrieving ACLs for document '"
<< doc_listing[index]["title"] << "': " << url << "\n\n";
service.ListAcls(url);
break;
case BACK:
// pass through
break;
default:
cerr << "Error - unknown sub-menu selection\n";
exit(1);
}
} while (sub_menu_choice != BACK);
break;
case SEARCH_DOCS:
url = doc_list_feed;
cout << "Enter full text query: ";
cin.ignore();
getline(cin, q);
url += "?q=" + StringUtils::find_and_replace(q, " ", "%20");
cout << "Searching documents for '" << q << "': " << url << "\n\n";
service.ListDocuments(url);
break;
case UPLOAD_DOC:
cout << "File name to upload: ";
cin >> doc_path;
cout << "Enter a document title: ";
cin.ignore();
getline(cin, doc_title);
service.UploadDoc(doc_path, doc_title);
break;
case DELETE_DOC:
url = doc_list_feed + "?showfolders=true";
doc_listing = service.ListDocuments(url, false);
for (unsigned int row = 0; row < doc_listing.size(); ++row) {
cout << row + 1 << ". [" << doc_listing[row]["doc_type"]
<< "] "<< doc_listing[row]["title"] << endl;
}
cout << "\nDelete which document? ";
index = GetMenuChoice(doc_listing.size()) - 1;
service.DeleteDoc(doc_listing[index]["edit_link"],
doc_listing[index]["etag"]);
break;
case EXIT:
cout << "\nGoodbye :)\n\n";
exit(0);
break;
default:
cerr << "Error - unknown menu selection";
exit(1);
}
}
return 0;
}
void PrintMainMenu() {
cout << endl << "Main Menu" << endl
<< setfill('-') << setw(40) << "-" << endl;
for (int i = 0; i < NUM_MAIN_MENU_OPTIONS; ++i) {
cout << i + 1 << ". " << MAIN_MENU[i] << endl;
}
cout << endl;
}
void PrintListDocSubMenu() {
string title = "List documents sub-menu";
cout << endl << title << endl
<< setfill('-') << setw(title.length()) << "-" << endl;
for (int i = 0; i < NUM_LIST_DOC_OPTIONS; ++i) {
cout << i + 1 << ". " << LIST_DOC_MENU[i] << endl;
}
cout << endl;
}
int GetMenuChoice(int num_options) {
bool good_choice = false;
int choice;
do {
cout << "> ";
cin >> choice;
if (!cin || (choice < 1 || choice > num_options)) {
cin.clear();
cin.ignore(1000, '\n');
cout << "Invalid selection. Please select 1-" << num_options << endl;
} else {
good_choice = true;
}
} while (!good_choice);
return choice;
}