summaryrefslogtreecommitdiffstats
path: root/chrome/browser/bookmarks/bookmark_drag_data.h
blob: 263bf8d15fd90f5fed8a6e2355b5f6bf150476c4 (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
// Copyright (c) 2006-2008 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.

#ifndef CHROME_BROWSER_BOOKMARKS_BOOKMARK_DRAG_DATA_
#define CHROME_BROWSER_BOOKMARKS_BOOKMARK_DRAG_DATA_

#include <vector>

#include "chrome/browser/history/history.h"
#include "googleurl/src/gurl.h"

class BookmarkModel;
class BookmarkNode;
class OSExchangeData;
class Pickle;
class Profile;

// BookmarkDragData is used to represent the following:
//
// . A single URL.
// . A single node from the bookmark model.
// . A set of nodes from the bookmark model.
//
// BookmarkDragData is used by bookmark related views to represent a dragged
// bookmark or bookmarks.
//
// Typical usage when writing data for a drag is:
//   BookmarkDragData data(node_user_is_dragging);
//   data.Write(os_exchange_data_for_drag);
//
// Typical usage to read is:
//   BookmarkDragData data;
//   if (data.Read(os_exchange_data))
//     // data is valid, contents are in elements.

struct BookmarkDragData {

  // Element represents a single node.
  struct Element {
    explicit Element(BookmarkNode* node);

    Element() : is_url(false), id_(0) {}

    // If true, this element represents a URL.
    bool is_url;

    // The URL, only valid if is_url is true.
    GURL url;

    // Title of the entry, used for both urls and groups/folders.
    std::wstring title;

    // Children, only used for non-URL nodes.
    std::vector<Element> children;

   private:
    friend struct BookmarkDragData;

    // For reading/writing this Element.
    void WriteToPickle(Pickle* pickle) const;
    bool ReadFromPickle(Pickle* pickle, void** iterator);

    // ID of the node.
    int id_;
  };

  BookmarkDragData() { }

  // Created a BookmarkDragData populated from the arguments.
  explicit BookmarkDragData(BookmarkNode* node);
  explicit BookmarkDragData(const std::vector<BookmarkNode*>& nodes);

  // Writes elements to data. If there is only one element and it is a URL
  // the URL and title are written to the clipboard in a format other apps can
  // use.
  void Write(Profile* profile, OSExchangeData* data) const;

  // Restores this data from the clipboard, returning true on success.
  bool Read(const OSExchangeData& data);

  // Returns the nodes represented by this DragData. If this DragData was
  // created from the same profile then the nodes from the model are returned.
  // If the nodes can't be found (may have been deleted), an empty vector is
  // returned.
  std::vector<BookmarkNode*> GetNodes(Profile* profile) const;

  // Convenience for getting the first node. Returns NULL if the data doesn't
  // match any nodes or there is more than one node.
  BookmarkNode* GetFirstNode(Profile* profile) const;

  // Do we contain valid data?
  bool is_valid() const { return !elements.empty(); }

  // Returns true if there is a single url.
  bool has_single_url() const { return is_valid() && elements[0].is_url; }

  // Number of elements.
  size_t size() const { return elements.size(); }

  // Returns true if this data is from the specified profile.
  bool IsFromProfile(Profile* profile) const;

  // The actual elements written to the clipboard.
  std::vector<Element> elements;

 private:
  // Path of the profile we originated from.
  std::wstring profile_path_;
};

#endif  // CHROME_BROWSER_BOOKMARKS_BOOKMARK_DRAG_DATA_