OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/ui/intents/intents_model.h" | |
6 #include "base/string_split.h" | |
7 #include "base/string_util.h" | |
8 #include "base/stringprintf.h" | |
9 #include "base/utf_string_conversions.h" | |
10 #include "chrome/browser/intents/web_intents_registry.h" | |
11 | |
12 IntentsModel::IntentsModel(WebIntentsRegistry* intents_registry) | |
13 : ui::TreeNodeModel<IntentsTreeNode>(new IntentsTreeNode()), | |
14 intents_registry_(intents_registry), | |
15 batch_update_(0) { | |
16 LoadModel(); | |
17 } | |
18 | |
19 void IntentsModel::AddIntentsTreeObserver(Observer* observer) { | |
20 intents_observer_list_.AddObserver(observer); | |
21 // Call super so that TreeNodeModel can notify, too. | |
22 ui::TreeNodeModel<IntentsTreeNode>::AddObserver(observer); | |
23 } | |
24 | |
25 void IntentsModel::RemoveIntentsTreeObserver(Observer* observer) { | |
26 intents_observer_list_.RemoveObserver(observer); | |
27 // Call super so that TreeNodeModel doesn't have dead pointers. | |
28 ui::TreeNodeModel<IntentsTreeNode>::RemoveObserver(observer); | |
29 } | |
30 | |
31 string16 IntentsModel::GetTreeNodeId(IntentsTreeNode* node) { | |
32 if (node->Type() == IntentsTreeNode::TYPE_ORIGIN) | |
33 return node->GetTitle(); | |
34 if (node->Type() == IntentsTreeNode::TYPE_SERVICE) { | |
35 return string16(); | |
36 } | |
37 | |
38 return string16(); | |
39 } | |
40 | |
41 IntentsTreeNode* IntentsModel::GetTreeNode(std::string path_id) { | |
42 if (path_id == "") | |
43 return GetRoot(); | |
44 | |
45 std::vector<std::string> node_ids; | |
46 base::SplitString(path_id, ',', &node_ids); | |
47 | |
48 for (int i = 0; i < GetRoot()->child_count(); ++i) { | |
49 IntentsTreeNode* node = GetRoot()->GetChild(i); | |
50 if (UTF16ToUTF8(node->GetTitle()) == node_ids[0]) { | |
51 if (node_ids.size() == 1) | |
52 return node; | |
53 } | |
54 } | |
55 | |
56 // TODO: support service nodes? | |
57 return NULL; | |
58 } | |
59 | |
60 void IntentsModel::GetChildNodeList(IntentsTreeNode* parent, | |
61 int start, int count, | |
62 base::ListValue* nodes) { | |
63 for (int i = 0; i < count; ++i) { | |
64 base::DictionaryValue* dict = new base::DictionaryValue; | |
65 IntentsTreeNode* child = parent->GetChild(start + i); | |
66 GetIntentsTreeNodeDictionary(*child, dict); | |
67 nodes->Append(dict); | |
68 } | |
69 } | |
70 | |
71 void IntentsModel::GetIntentsTreeNodeDictionary(const IntentsTreeNode& node, | |
72 base::DictionaryValue* dict) { | |
73 if (node.Type() == IntentsTreeNode::TYPE_ROOT) { | |
74 return; | |
75 } | |
76 | |
77 if (node.Type() == IntentsTreeNode::TYPE_ORIGIN) { | |
78 dict->SetString("site", node.GetTitle()); | |
79 dict->SetBoolean("hasChildren", node.child_count() > 0); | |
80 return; | |
81 } | |
82 | |
83 if (node.Type() == IntentsTreeNode::TYPE_SERVICE) { | |
84 const ServiceTreeNode* snode = static_cast<const ServiceTreeNode*>(&node); | |
85 dict->SetString("site", snode->GetTitle()); | |
86 dict->SetString("name", snode->ServiceName()); | |
87 dict->SetString("url", snode->ServiceUrl()); | |
88 dict->SetString("icon", snode->IconUrl()); | |
89 dict->SetString("action", snode->Action()); | |
90 dict->Set("types", snode->Types().DeepCopy()); | |
91 dict->SetBoolean("blocked", snode->IsBlocked()); | |
92 dict->SetBoolean("disabled", snode->IsDisabled()); | |
93 return; | |
94 } | |
95 } | |
96 | |
97 void IntentsModel::LoadModel() { | |
98 NotifyObserverBeginBatch(); | |
99 intents_registry_->GetAllIntentProviders(this); | |
100 } | |
101 | |
102 void IntentsModel::OnIntentsQueryDone( | |
103 WebIntentsRegistry::QueryID query_id, | |
104 const std::vector<WebIntentData>& intents) OVERRIDE { | |
105 for (size_t i = 0; i < intents.size(); ++i) { | |
106 // Eventually do some awesome sorting, grouping, clustering stuff here. | |
107 // For now, just stick it in the model flat. | |
108 IntentsTreeNode* n = new IntentsTreeNode(ASCIIToUTF16( | |
109 intents[i].service_url.host())); | |
110 ServiceTreeNode* ns = new ServiceTreeNode(ASCIIToUTF16( | |
111 intents[i].service_url.host())); | |
112 ns->SetServiceName(intents[i].title); | |
113 ns->SetServiceUrl(ASCIIToUTF16(intents[i].service_url.spec())); | |
114 GURL icon_url = intents[i].service_url.GetOrigin().Resolve("/favicon.ico"); | |
115 ns->SetIconUrl(ASCIIToUTF16(icon_url.spec())); | |
116 ns->SetAction(intents[i].action); | |
117 ns->AddType(intents[i].type); | |
118 // Won't generate a notification. OK for now as the next line will. | |
119 n->Add(ns, 0); | |
120 Add(GetRoot(), n, GetRoot()->child_count()); | |
121 } | |
122 | |
123 // !!!!!!!!!!!! artificially load the intents model if nothing exists. | |
124 /* | |
125 if (intents.size() == 0) { | |
James Hawkins
2011/08/17 03:18:39
Remove this block now that we have the registry.
Greg Billock
2011/08/17 18:49:50
Done.
| |
126 { | |
127 WebIntentData provider; | |
128 provider.service_url = GURL("http://www.google.com/share"); | |
129 provider.action = ASCIIToUTF16("SHARE"); | |
130 provider.type = ASCIIToUTF16("text/url"); | |
131 provider.title = ASCIIToUTF16("Google"); | |
132 intents_registry_->RegisterIntentProvider(provider); | |
133 } | |
134 { | |
135 WebIntentData provider; | |
136 provider.service_url = GURL("http://picasaweb.google.com/share"); | |
137 provider.action = ASCIIToUTF16("EDIT"); | |
138 provider.type = ASCIIToUTF16("image/-"); | |
139 provider.title = ASCIIToUTF16("Picasa"); | |
140 intents_registry_->RegisterIntentProvider(provider); | |
141 } | |
142 { | |
143 WebIntentData provider; | |
144 provider.service_url = GURL("http://www.digg.com/share"); | |
145 provider.action = ASCIIToUTF16("SHARE"); | |
146 provider.type = ASCIIToUTF16("text/url"); | |
147 provider.title = ASCIIToUTF16("Digg"); | |
148 intents_registry_->RegisterIntentProvider(provider); | |
149 } | |
150 intents_registry_->GetAllIntentProviders(this); | |
151 } | |
152 // Delete to here... | |
153 */ | |
154 | |
155 NotifyObserverEndBatch(); | |
156 } | |
157 | |
158 void IntentsModel::NotifyObserverBeginBatch() { | |
159 // Only notify the model once if we're batching in a nested manner. | |
160 if (batch_update_++ == 0) { | |
161 FOR_EACH_OBSERVER(Observer, | |
162 intents_observer_list_, | |
163 TreeModelBeginBatch(this)); | |
164 } | |
165 } | |
166 | |
167 void IntentsModel::NotifyObserverEndBatch() { | |
168 // Only notify the observers if this is the outermost call to EndBatch() if | |
169 // called in a nested manner. | |
170 if (--batch_update_ == 0) { | |
171 FOR_EACH_OBSERVER(Observer, | |
172 intents_observer_list_, | |
173 TreeModelEndBatch(this)); | |
174 } | |
175 } | |
OLD | NEW |