Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / applescript / bookmark_folder_applescript.mm
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 #import "chrome/browser/ui/cocoa/applescript/bookmark_folder_applescript.h"
6
7 #import "base/mac/scoped_nsobject.h"
8 #import "base/strings/string16.h"
9 #include "base/strings/sys_string_conversions.h"
10 #import "chrome/browser/ui/cocoa/applescript/bookmark_item_applescript.h"
11 #import "chrome/browser/ui/cocoa/applescript/constants_applescript.h"
12 #include "chrome/browser/ui/cocoa/applescript/error_applescript.h"
13 #include "components/bookmarks/core/browser/bookmark_model.h"
14 #include "url/gurl.h"
15
16 @implementation BookmarkFolderAppleScript
17
18 - (NSArray*)bookmarkFolders {
19   NSMutableArray* bookmarkFolders = [NSMutableArray
20       arrayWithCapacity:bookmarkNode_->child_count()];
21
22   for (int i = 0; i < bookmarkNode_->child_count(); ++i) {
23     const BookmarkNode* node = bookmarkNode_->GetChild(i);
24
25     if (!node->is_folder())
26       continue;
27     base::scoped_nsobject<BookmarkFolderAppleScript> bookmarkFolder(
28         [[BookmarkFolderAppleScript alloc] initWithBookmarkNode:node]);
29     [bookmarkFolder setContainer:self
30                         property:AppleScript::kBookmarkFoldersProperty];
31     [bookmarkFolders addObject:bookmarkFolder];
32   }
33
34   return bookmarkFolders;
35 }
36
37 - (void)insertInBookmarkFolders:(id)aBookmarkFolder {
38   // This method gets called when a new bookmark folder is created so
39   // the container and property are set here.
40   [aBookmarkFolder setContainer:self
41                        property:AppleScript::kBookmarkFoldersProperty];
42   BookmarkModel* model = [self bookmarkModel];
43   if (!model)
44     return;
45
46   const BookmarkNode* node = model->AddFolder(bookmarkNode_,
47                                               bookmarkNode_->child_count(),
48                                               base::string16());
49   if (!node) {
50     AppleScript::SetError(AppleScript::errCreateBookmarkFolder);
51     return;
52   }
53
54   [aBookmarkFolder setBookmarkNode:node];
55 }
56
57 - (void)insertInBookmarkFolders:(id)aBookmarkFolder atIndex:(int)index {
58   // This method gets called when a new bookmark folder is created so
59   // the container and property are set here.
60   [aBookmarkFolder setContainer:self
61                        property:AppleScript::kBookmarkFoldersProperty];
62   int position = [self calculatePositionOfBookmarkFolderAt:index];
63
64   BookmarkModel* model = [self bookmarkModel];
65   if (!model)
66     return;
67
68   const BookmarkNode* node = model->AddFolder(bookmarkNode_,
69                                               position,
70                                               base::string16());
71   if (!node) {
72     AppleScript::SetError(AppleScript::errCreateBookmarkFolder);
73     return;
74   }
75
76   [aBookmarkFolder setBookmarkNode:node];
77 }
78
79 - (void)removeFromBookmarkFoldersAtIndex:(int)index {
80   int position = [self calculatePositionOfBookmarkFolderAt:index];
81
82   BookmarkModel* model = [self bookmarkModel];
83   if (!model)
84     return;
85
86   model->Remove(bookmarkNode_, position);
87 }
88
89 - (NSArray*)bookmarkItems {
90   NSMutableArray* bookmarkItems = [NSMutableArray
91       arrayWithCapacity:bookmarkNode_->child_count()];
92
93   for (int i = 0; i < bookmarkNode_->child_count(); ++i) {
94     const BookmarkNode* node = bookmarkNode_->GetChild(i);
95
96     if (!node->is_url())
97       continue;
98     base::scoped_nsobject<BookmarkItemAppleScript> bookmarkItem(
99         [[BookmarkItemAppleScript alloc] initWithBookmarkNode:node]);
100     [bookmarkItem setContainer:self
101                       property:AppleScript::kBookmarkItemsProperty];
102     [bookmarkItems addObject:bookmarkItem];
103   }
104
105   return bookmarkItems;
106 }
107
108 - (void)insertInBookmarkItems:(BookmarkItemAppleScript*)aBookmarkItem {
109   // This method gets called when a new bookmark item is created so
110   // the container and property are set here.
111   [aBookmarkItem setContainer:self
112                      property:AppleScript::kBookmarkItemsProperty];
113
114   BookmarkModel* model = [self bookmarkModel];
115   if (!model)
116     return;
117
118   GURL url = GURL(base::SysNSStringToUTF8([aBookmarkItem URL]));
119   if (!url.is_valid()) {
120     AppleScript::SetError(AppleScript::errInvalidURL);
121     return;
122   }
123
124   const BookmarkNode* node = model->AddURL(bookmarkNode_,
125                                            bookmarkNode_->child_count(),
126                                            base::string16(),
127                                            url);
128   if (!node) {
129     AppleScript::SetError(AppleScript::errCreateBookmarkItem);
130     return;
131   }
132
133   [aBookmarkItem setBookmarkNode:node];
134 }
135
136 - (void)insertInBookmarkItems:(BookmarkItemAppleScript*)aBookmarkItem
137                       atIndex:(int)index {
138   // This method gets called when a new bookmark item is created so
139   // the container and property are set here.
140   [aBookmarkItem setContainer:self
141                      property:AppleScript::kBookmarkItemsProperty];
142   int position = [self calculatePositionOfBookmarkItemAt:index];
143
144   BookmarkModel* model = [self bookmarkModel];
145   if (!model)
146     return;
147
148   GURL url(base::SysNSStringToUTF8([aBookmarkItem URL]));
149   if (!url.is_valid()) {
150     AppleScript::SetError(AppleScript::errInvalidURL);
151     return;
152   }
153
154   const BookmarkNode* node = model->AddURL(bookmarkNode_,
155                                            position,
156                                            base::string16(),
157                                            url);
158   if (!node) {
159     AppleScript::SetError(AppleScript::errCreateBookmarkItem);
160     return;
161   }
162
163   [aBookmarkItem setBookmarkNode:node];
164 }
165
166 - (void)removeFromBookmarkItemsAtIndex:(int)index {
167   int position = [self calculatePositionOfBookmarkItemAt:index];
168
169   BookmarkModel* model = [self bookmarkModel];
170   if (!model)
171     return;
172
173   model->Remove(bookmarkNode_, position);
174 }
175
176 - (int)calculatePositionOfBookmarkFolderAt:(int)index {
177   // Traverse through all the child nodes till the required node is found and
178   // return its position.
179   // AppleScript is 1-based therefore index is incremented by 1.
180   ++index;
181   int count = -1;
182   while (index) {
183     if (bookmarkNode_->GetChild(++count)->is_folder())
184       --index;
185   }
186   return count;
187 }
188
189 - (int)calculatePositionOfBookmarkItemAt:(int)index {
190   // Traverse through all the child nodes till the required node is found and
191   // return its position.
192   // AppleScript is 1-based therefore index is incremented by 1.
193   ++index;
194   int count = -1;
195   while (index) {
196     if (bookmarkNode_->GetChild(++count)->is_url())
197       --index;
198   }
199   return count;
200 }
201
202 @end