Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(84)

Side by Side Diff: google_apis/drive/gdata_wapi_parser.cc

Issue 384543004: Get rid of DriveEntryKind. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Removed some temporary variables and IsHostedDocumentByFileExtension(). Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « google_apis/drive/gdata_wapi_parser.h ('k') | google_apis/drive/gdata_wapi_parser_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "google_apis/drive/gdata_wapi_parser.h" 5 #include "google_apis/drive/gdata_wapi_parser.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 9
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 const char kTermField[] = "term"; 65 const char kTermField[] = "term";
66 const char kTitleField[] = "title"; 66 const char kTitleField[] = "title";
67 const char kTitleTField[] = "title.$t"; 67 const char kTitleTField[] = "title.$t";
68 const char kTypeField[] = "type"; 68 const char kTypeField[] = "type";
69 const char kUpdatedField[] = "updated.$t"; 69 const char kUpdatedField[] = "updated.$t";
70 70
71 // Link Prefixes 71 // Link Prefixes
72 const char kOpenWithPrefix[] = "http://schemas.google.com/docs/2007#open-with-"; 72 const char kOpenWithPrefix[] = "http://schemas.google.com/docs/2007#open-with-";
73 const size_t kOpenWithPrefixSize = arraysize(kOpenWithPrefix) - 1; 73 const size_t kOpenWithPrefixSize = arraysize(kOpenWithPrefix) - 1;
74 74
75 struct EntryKindMap {
76 DriveEntryKind kind;
77 const char* entry;
78 const char* extension;
79 };
80
81 const EntryKindMap kEntryKindMap[] = {
82 { ENTRY_KIND_UNKNOWN, "unknown", NULL},
83 { ENTRY_KIND_ITEM, "item", NULL},
84 { ENTRY_KIND_DOCUMENT, "document", ".gdoc"},
85 { ENTRY_KIND_SPREADSHEET, "spreadsheet", ".gsheet"},
86 { ENTRY_KIND_PRESENTATION, "presentation", ".gslides" },
87 { ENTRY_KIND_DRAWING, "drawing", ".gdraw"},
88 { ENTRY_KIND_TABLE, "table", ".gtable"},
89 { ENTRY_KIND_FORM, "form", ".gform"},
90 { ENTRY_KIND_EXTERNAL_APP, "externalapp", ".glink"},
91 { ENTRY_KIND_SITE, "site", NULL},
92 { ENTRY_KIND_FOLDER, "folder", NULL},
93 { ENTRY_KIND_FILE, "file", NULL},
94 { ENTRY_KIND_PDF, "pdf", NULL},
95 };
96 COMPILE_ASSERT(arraysize(kEntryKindMap) == ENTRY_KIND_MAX_VALUE,
97 EntryKindMap_and_DriveEntryKind_are_not_in_sync);
98
99 struct LinkTypeMap { 75 struct LinkTypeMap {
100 Link::LinkType type; 76 Link::LinkType type;
101 const char* rel; 77 const char* rel;
102 }; 78 };
103 79
104 const LinkTypeMap kLinkTypeMap[] = { 80 const LinkTypeMap kLinkTypeMap[] = {
105 { Link::LINK_SELF, 81 { Link::LINK_SELF,
106 "self" }, 82 "self" },
107 { Link::LINK_NEXT, 83 { Link::LINK_NEXT,
108 "next" }, 84 "next" },
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after
432 kDeletedField, &ResourceEntry::deleted_, &ResourceEntry::HasFieldPresent); 408 kDeletedField, &ResourceEntry::deleted_, &ResourceEntry::HasFieldPresent);
433 converter->RegisterCustomValueField<bool>( 409 converter->RegisterCustomValueField<bool>(
434 kRemovedField, &ResourceEntry::removed_, &ResourceEntry::HasFieldPresent); 410 kRemovedField, &ResourceEntry::removed_, &ResourceEntry::HasFieldPresent);
435 converter->RegisterCustomValueField<int64>( 411 converter->RegisterCustomValueField<int64>(
436 kChangestampField, &ResourceEntry::changestamp_, 412 kChangestampField, &ResourceEntry::changestamp_,
437 &ResourceEntry::ParseChangestamp); 413 &ResourceEntry::ParseChangestamp);
438 // ImageMediaMetadata fields are not supported by WAPI. 414 // ImageMediaMetadata fields are not supported by WAPI.
439 } 415 }
440 416
441 // static 417 // static
442 std::string ResourceEntry::GetHostedDocumentExtension(DriveEntryKind kind) { 418 ResourceEntry::ResourceEntryKind ResourceEntry::GetEntryKindFromTerm(
443 for (size_t i = 0; i < arraysize(kEntryKindMap); i++) {
444 if (kEntryKindMap[i].kind == kind) {
445 if (kEntryKindMap[i].extension)
446 return std::string(kEntryKindMap[i].extension);
447 else
448 return std::string();
449 }
450 }
451 return std::string();
452 }
453
454 // static
455 DriveEntryKind ResourceEntry::GetEntryKindFromExtension(
456 const std::string& extension) {
457 for (size_t i = 0; i < arraysize(kEntryKindMap); ++i) {
458 const char* document_extension = kEntryKindMap[i].extension;
459 if (document_extension && extension == document_extension)
460 return kEntryKindMap[i].kind;
461 }
462 return ENTRY_KIND_UNKNOWN;
463 }
464
465 // static
466 int ResourceEntry::ClassifyEntryKindByFileExtension(
467 const base::FilePath& file_path) {
468 #if defined(OS_WIN)
469 std::string file_extension = base::WideToUTF8(file_path.Extension());
470 #else
471 std::string file_extension = file_path.Extension();
472 #endif
473 return ClassifyEntryKind(GetEntryKindFromExtension(file_extension));
474 }
475
476 // static
477 DriveEntryKind ResourceEntry::GetEntryKindFromTerm(
478 const std::string& term) { 419 const std::string& term) {
479 if (!StartsWithASCII(term, kTermPrefix, false)) { 420 if (!StartsWithASCII(term, kTermPrefix, false)) {
480 DVLOG(1) << "Unexpected term prefix term " << term; 421 DVLOG(1) << "Unexpected term prefix term " << term;
481 return ENTRY_KIND_UNKNOWN; 422 return ENTRY_KIND_UNKNOWN;
482 } 423 }
483 424
484 std::string type = term.substr(strlen(kTermPrefix)); 425 std::string type = term.substr(strlen(kTermPrefix));
485 for (size_t i = 0; i < arraysize(kEntryKindMap); i++) { 426 if (type == "folder")
486 if (type == kEntryKindMap[i].entry) 427 return ENTRY_KIND_FOLDER;
487 return kEntryKindMap[i].kind; 428 if (type == "file" || type == "pdf")
488 } 429 return ENTRY_KIND_FILE;
430
489 DVLOG(1) << "Unknown entry type for term " << term << ", type " << type; 431 DVLOG(1) << "Unknown entry type for term " << term << ", type " << type;
490 return ENTRY_KIND_UNKNOWN; 432 return ENTRY_KIND_UNKNOWN;
491 } 433 }
492 434
493 // static
494 int ResourceEntry::ClassifyEntryKind(DriveEntryKind kind) {
495 int classes = 0;
496
497 // All DriveEntryKind members are listed here, so the compiler catches if a
498 // newly added member is missing here.
499 switch (kind) {
500 case ENTRY_KIND_UNKNOWN:
501 // Special entries.
502 case ENTRY_KIND_ITEM:
503 case ENTRY_KIND_SITE:
504 break;
505
506 // Hosted Google document.
507 case ENTRY_KIND_DOCUMENT:
508 case ENTRY_KIND_SPREADSHEET:
509 case ENTRY_KIND_PRESENTATION:
510 case ENTRY_KIND_DRAWING:
511 case ENTRY_KIND_TABLE:
512 case ENTRY_KIND_FORM:
513 classes = KIND_OF_GOOGLE_DOCUMENT | KIND_OF_HOSTED_DOCUMENT;
514 break;
515
516 // Hosted external application document.
517 case ENTRY_KIND_EXTERNAL_APP:
518 classes = KIND_OF_EXTERNAL_DOCUMENT | KIND_OF_HOSTED_DOCUMENT;
519 break;
520
521 // Folders, collections.
522 case ENTRY_KIND_FOLDER:
523 classes = KIND_OF_FOLDER;
524 break;
525
526 // Regular files.
527 case ENTRY_KIND_FILE:
528 case ENTRY_KIND_PDF:
529 classes = KIND_OF_FILE;
530 break;
531
532 case ENTRY_KIND_MAX_VALUE:
533 NOTREACHED();
534 }
535
536 return classes;
537 }
538
539 void ResourceEntry::FillRemainingFields() { 435 void ResourceEntry::FillRemainingFields() {
540 // Set |kind_| and |labels_| based on the |categories_| in the class. 436 // Set |kind_| and |labels_| based on the |categories_| in the class.
541 // JSONValueConverter does not have the ability to catch an element in a list 437 // JSONValueConverter does not have the ability to catch an element in a list
542 // based on a predicate. Thus we need to iterate over |categories_| and 438 // based on a predicate. Thus we need to iterate over |categories_| and
543 // find the elements to set these fields as a post-process. 439 // find the elements to set these fields as a post-process.
544 for (size_t i = 0; i < categories_.size(); ++i) { 440 for (size_t i = 0; i < categories_.size(); ++i) {
545 const Category* category = categories_[i]; 441 const Category* category = categories_[i];
546 if (category->type() == Category::CATEGORY_KIND) 442 if (category->type() == Category::CATEGORY_KIND)
547 kind_ = GetEntryKindFromTerm(category->term()); 443 kind_ = GetEntryKindFromTerm(category->term());
548 else if (category->type() == Category::CATEGORY_LABEL) 444 else if (category->type() == Category::CATEGORY_LABEL)
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
658 } 554 }
659 } 555 }
660 return false; 556 return false;
661 } 557 }
662 558
663 void ResourceList::ReleaseEntries(std::vector<ResourceEntry*>* entries) { 559 void ResourceList::ReleaseEntries(std::vector<ResourceEntry*>* entries) {
664 entries_.release(entries); 560 entries_.release(entries);
665 } 561 }
666 562
667 } // namespace google_apis 563 } // namespace google_apis
OLDNEW
« no previous file with comments | « google_apis/drive/gdata_wapi_parser.h ('k') | google_apis/drive/gdata_wapi_parser_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698