OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/custom_home_pages_table_model.h" | 5 #include "chrome/browser/custom_home_pages_table_model.h" |
6 | 6 |
7 #include "base/i18n/rtl.h" | 7 #include "base/i18n/rtl.h" |
8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
9 #include "chrome/browser/prefs/pref_service.h" | 9 #include "chrome/browser/prefs/pref_service.h" |
10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 entries_[i].url = urls[i]; | 59 entries_[i].url = urls[i]; |
60 entries_[i].title.erase(); | 60 entries_[i].title.erase(); |
61 entries_[i].icon.reset(); | 61 entries_[i].icon.reset(); |
62 LoadTitleAndFavicon(&(entries_[i])); | 62 LoadTitleAndFavicon(&(entries_[i])); |
63 } | 63 } |
64 // Complete change, so tell the view to just rebuild itself. | 64 // Complete change, so tell the view to just rebuild itself. |
65 if (observer_) | 65 if (observer_) |
66 observer_->OnModelChanged(); | 66 observer_->OnModelChanged(); |
67 } | 67 } |
68 | 68 |
| 69 /** |
| 70 * Move a number of existing entries to a new position, reordering the table. |
| 71 * |
| 72 * We determine the range of elements affected by the move, save the moved |
| 73 * elements, compact the remaining ones, and re-insert moved elements. |
| 74 * Expects |index_list| to be ordered ascending. |
| 75 */ |
| 76 void CustomHomePagesTableModel::MoveURLs(int insert_before, |
| 77 const std::vector<int>& index_list) |
| 78 { |
| 79 DCHECK(insert_before >= 0 && insert_before <= RowCount()); |
| 80 |
| 81 // The range of elements that needs to be reshuffled is [ |first|, |last| ). |
| 82 int first = std::min(insert_before, index_list.front()); |
| 83 int last = std::max(insert_before, index_list.back() + 1); |
| 84 |
| 85 // Save the dragged elements. Also, adjust insertion point if it is before a |
| 86 // dragged element. |
| 87 std::vector<Entry> moved_entries; |
| 88 for (size_t i = 0; i < index_list.size(); ++i) { |
| 89 moved_entries.push_back(entries_[index_list[i]]); |
| 90 if (index_list[i] == insert_before) |
| 91 insert_before++; |
| 92 } |
| 93 |
| 94 // Compact the range between beginning and insertion point, moving downwards. |
| 95 size_t skip_count = 0; |
| 96 for (int i = first; i < insert_before; ++i) { |
| 97 if (skip_count < index_list.size() && index_list[skip_count] == i) |
| 98 skip_count++; |
| 99 else |
| 100 entries_[i - skip_count]=entries_[i]; |
| 101 } |
| 102 |
| 103 // Moving items down created a gap. We start compacting up after it. |
| 104 first = insert_before; |
| 105 insert_before -= skip_count; |
| 106 |
| 107 // Now compact up for elements after the insertion point. |
| 108 skip_count = 0; |
| 109 for (int i = last - 1; i >= first; --i) { |
| 110 if (skip_count < index_list.size() && |
| 111 index_list[index_list.size() - skip_count - 1] == i) { |
| 112 skip_count++; |
| 113 } else { |
| 114 entries_[i + skip_count] = entries_[i]; |
| 115 } |
| 116 } |
| 117 |
| 118 // Insert moved elements. |
| 119 std::copy(moved_entries.begin(), moved_entries.end(), |
| 120 entries_.begin() + insert_before); |
| 121 |
| 122 // Possibly large change, so tell the view to just rebuild itself. |
| 123 if (observer_) |
| 124 observer_->OnModelChanged(); |
| 125 } |
| 126 |
69 void CustomHomePagesTableModel::Add(int index, const GURL& url) { | 127 void CustomHomePagesTableModel::Add(int index, const GURL& url) { |
70 DCHECK(index >= 0 && index <= RowCount()); | 128 DCHECK(index >= 0 && index <= RowCount()); |
71 entries_.insert(entries_.begin() + static_cast<size_t>(index), Entry()); | 129 entries_.insert(entries_.begin() + static_cast<size_t>(index), Entry()); |
72 entries_[index].url = url; | 130 entries_[index].url = url; |
73 LoadTitleAndFavicon(&(entries_[index])); | 131 LoadTitleAndFavicon(&(entries_[index])); |
74 if (observer_) | 132 if (observer_) |
75 observer_->OnItemsAdded(index, 1); | 133 observer_->OnItemsAdded(index, 1); |
76 } | 134 } |
77 | 135 |
78 void CustomHomePagesTableModel::Remove(int index) { | 136 void CustomHomePagesTableModel::Remove(int index) { |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 return NULL; | 288 return NULL; |
231 } | 289 } |
232 | 290 |
233 string16 CustomHomePagesTableModel::FormattedURL(int row) const { | 291 string16 CustomHomePagesTableModel::FormattedURL(int row) const { |
234 std::string languages = | 292 std::string languages = |
235 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages); | 293 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages); |
236 string16 url = net::FormatUrl(entries_[row].url, languages); | 294 string16 url = net::FormatUrl(entries_[row].url, languages); |
237 url = base::i18n::GetDisplayStringInLTRDirectionality(url); | 295 url = base::i18n::GetDisplayStringInLTRDirectionality(url); |
238 return url; | 296 return url; |
239 } | 297 } |
OLD | NEW |