Index: google_apis/drive/drive_entry_kinds.cc |
diff --git a/google_apis/drive/drive_entry_kinds.cc b/google_apis/drive/drive_entry_kinds.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..799f1e841b30486cbc8c3435a65ef4e855dc551d |
--- /dev/null |
+++ b/google_apis/drive/drive_entry_kinds.cc |
@@ -0,0 +1,116 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "google_apis/drive/drive_entry_kinds.h" |
+ |
+#include "base/files/file_path.h" |
+#include "base/logging.h" |
+#include "base/macros.h" |
+ |
+namespace google_apis { |
+ |
+namespace { |
+ |
+struct EntryKindMap { |
+ DriveEntryKind kind; |
+ const char* extension; |
+}; |
+ |
+const EntryKindMap kEntryKindMap[] = { |
+ { ENTRY_KIND_UNKNOWN, NULL}, |
+ { ENTRY_KIND_ITEM, NULL}, |
+ { ENTRY_KIND_DOCUMENT, ".gdoc"}, |
+ { ENTRY_KIND_SPREADSHEET, ".gsheet"}, |
+ { ENTRY_KIND_PRESENTATION, ".gslides" }, |
+ { ENTRY_KIND_DRAWING, ".gdraw"}, |
+ { ENTRY_KIND_TABLE, ".gtable"}, |
+ { ENTRY_KIND_FORM, ".gform"}, |
+ { ENTRY_KIND_EXTERNAL_APP, ".glink"}, |
+ { ENTRY_KIND_SITE, NULL}, |
+ { ENTRY_KIND_FOLDER, NULL}, |
+ { ENTRY_KIND_FILE, NULL}, |
+ { ENTRY_KIND_PDF, NULL}, |
+}; |
+ |
+COMPILE_ASSERT(arraysize(kEntryKindMap) == ENTRY_KIND_MAX_VALUE, |
+ EntryKindMap_and_DriveEntryKind_are_not_in_sync); |
+ |
+} // namespace |
+ |
+std::string GetHostedDocumentExtension(DriveEntryKind kind) { |
+ for (size_t i = 0; i < arraysize(kEntryKindMap); i++) { |
+ if (kEntryKindMap[i].kind == kind) { |
+ if (kEntryKindMap[i].extension) |
+ return std::string(kEntryKindMap[i].extension); |
+ else |
+ return std::string(); |
+ } |
+ } |
+ return std::string(); |
+} |
+ |
+DriveEntryKind GetEntryKindFromExtension(const std::string& extension) { |
+ for (size_t i = 0; i < arraysize(kEntryKindMap); ++i) { |
+ const char* document_extension = kEntryKindMap[i].extension; |
+ if (document_extension && extension == document_extension) |
+ return kEntryKindMap[i].kind; |
+ } |
+ return ENTRY_KIND_UNKNOWN; |
+} |
+ |
+int ClassifyEntryKindByFileExtension(const base::FilePath& file_path) { |
+#if defined(OS_WIN) |
+ std::string file_extension = base::WideToUTF8(file_path.Extension()); |
+#else |
+ std::string file_extension = file_path.Extension(); |
+#endif |
+ return ClassifyEntryKind(GetEntryKindFromExtension(file_extension)); |
+} |
+ |
+int ClassifyEntryKind(DriveEntryKind kind) { |
+ int classes = 0; |
+ |
+ // All DriveEntryKind members are listed here, so the compiler catches if a |
+ // newly added member is missing here. |
+ switch (kind) { |
+ case ENTRY_KIND_UNKNOWN: |
+ // Special entries. |
+ case ENTRY_KIND_ITEM: |
+ case ENTRY_KIND_SITE: |
+ break; |
+ |
+ // Hosted Google document. |
+ case ENTRY_KIND_DOCUMENT: |
+ case ENTRY_KIND_SPREADSHEET: |
+ case ENTRY_KIND_PRESENTATION: |
+ case ENTRY_KIND_DRAWING: |
+ case ENTRY_KIND_TABLE: |
+ case ENTRY_KIND_FORM: |
+ classes = KIND_OF_GOOGLE_DOCUMENT | KIND_OF_HOSTED_DOCUMENT; |
+ break; |
+ |
+ // Hosted external application document. |
+ case ENTRY_KIND_EXTERNAL_APP: |
+ classes = KIND_OF_EXTERNAL_DOCUMENT | KIND_OF_HOSTED_DOCUMENT; |
+ break; |
+ |
+ // Folders, collections. |
+ case ENTRY_KIND_FOLDER: |
+ classes = KIND_OF_FOLDER; |
+ break; |
+ |
+ // Regular files. |
+ case ENTRY_KIND_FILE: |
+ case ENTRY_KIND_PDF: |
+ classes = KIND_OF_FILE; |
+ break; |
+ |
+ case ENTRY_KIND_MAX_VALUE: |
+ NOTREACHED(); |
+ } |
+ |
+ return classes; |
+} |
+ |
+} // namespace google_apis |