1 // Copyright (c) 2012 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.
5 #include "chrome/browser/sync/sync_ui_util_mac.h"
7 #import <Cocoa/Cocoa.h>
9 #include "base/utf_string_conversions.h"
10 #include "base/logging.h"
11 #include "chrome/browser/prefs/pref_service.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/signin/signin_manager.h"
14 #include "chrome/browser/signin/signin_manager_factory.h"
15 #include "chrome/browser/sync/profile_sync_service.h"
16 #include "chrome/browser/sync/profile_sync_service_factory.h"
17 #include "chrome/browser/sync/sync_ui_util.h"
18 #include "chrome/common/pref_names.h"
19 #include "grit/chromium_strings.h"
20 #include "grit/generated_resources.h"
21 #include "ui/base/l10n/l10n_util.h"
22 #include "ui/base/l10n/l10n_util_mac.h"
24 using l10n_util::GetStringUTF16;
25 using l10n_util::GetNSStringWithFixup;
26 using l10n_util::GetNSStringFWithFixup;
28 namespace sync_ui_util {
30 void UpdateSyncItem(id syncItem, BOOL syncEnabled, Profile* profile) {
31 ProfileSyncService* syncService =
32 ProfileSyncServiceFactory::GetInstance()->GetForProfile(
33 profile->GetOriginalProfile());
34 SigninManager* signin = SigninManagerFactory::GetForProfile(profile);
35 UpdateSyncItemForStatus(
38 sync_ui_util::GetStatus(syncService, *signin),
39 profile->GetPrefs()->GetString(prefs::kGoogleServicesUsername));
42 void UpdateSyncItemForStatus(id syncItem, BOOL syncEnabled,
43 sync_ui_util::MessageType status,
44 const std::string& userName) {
45 DCHECK([syncItem isKindOfClass:[NSMenuItem class]]);
46 NSMenuItem* syncMenuItem = static_cast<NSMenuItem*>(syncItem);
47 // Look for a separator immediately after the menu item.
48 NSMenuItem* followingSeparator = nil;
49 NSMenu* menu = [syncItem menu];
51 NSInteger syncItemIndex = [menu indexOfItem:syncMenuItem];
52 DCHECK_NE(syncItemIndex, -1);
53 if ((syncItemIndex + 1) < [menu numberOfItems]) {
54 NSMenuItem* menuItem = [menu itemAtIndex:(syncItemIndex + 1)];
55 if ([menuItem isSeparatorItem]) {
56 followingSeparator = menuItem;
61 // TODO(akalin): consolidate this code with the equivalent Windows code in
62 // chrome/browser/ui/views/toolbar_view.cc.
63 NSString* title = NULL;
65 case sync_ui_util::SYNCED:
66 title = GetNSStringFWithFixup(IDS_SYNC_MENU_SYNCED_LABEL,
67 UTF8ToUTF16(userName));
69 case sync_ui_util::SYNC_ERROR:
70 title = GetNSStringWithFixup(IDS_SYNC_MENU_SYNC_ERROR_LABEL);
72 case sync_ui_util::PRE_SYNCED:
73 case sync_ui_util::SYNC_PROMO:
75 title = GetNSStringFWithFixup(IDS_SYNC_MENU_PRE_SYNCED_LABEL,
76 GetStringUTF16(IDS_SHORT_PRODUCT_NAME));
79 [syncMenuItem setTitle:title];
81 // If we don't have a sync service, hide any sync-related menu
82 // items. However, sync_menu_item is enabled/disabled outside of this
83 // function so we don't touch it here, and separators are always disabled.
84 [syncMenuItem setHidden:!syncEnabled];
85 [followingSeparator setHidden:!syncEnabled];
88 } // namespace sync_ui_util