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/sync/sync_ui_util_mac.h"
6
7#import <Cocoa/Cocoa.h>
8
9#include "base/logging.h"
10#include "chrome/browser/profiles/profile.h"
11#include "chrome/browser/sync/sync_ui_util.h"
12#include "grit/generated_resources.h"
13#include "ui/base/l10n/l10n_util_mac.h"
14
15namespace sync_ui_util {
16
17void UpdateSyncItem(id syncItem, BOOL syncEnabled, Profile* profile) {
18  ProfileSyncService* syncService =
19    profile->GetOriginalProfile()->GetProfileSyncService();
20  UpdateSyncItemForStatus(syncItem, syncEnabled,
21                          sync_ui_util::GetStatus(syncService));
22}
23
24void UpdateSyncItemForStatus(id syncItem, BOOL syncEnabled,
25                             sync_ui_util::MessageType status) {
26  DCHECK([syncItem isKindOfClass:[NSMenuItem class]]);
27  NSMenuItem* syncMenuItem = static_cast<NSMenuItem*>(syncItem);
28  // Look for a separator immediately after the menu item.
29  NSMenuItem* followingSeparator = nil;
30  NSMenu* menu = [syncItem menu];
31  if (menu) {
32    NSInteger syncItemIndex = [menu indexOfItem:syncMenuItem];
33    DCHECK_NE(syncItemIndex, -1);
34    if ((syncItemIndex + 1) < [menu numberOfItems]) {
35      NSMenuItem* menuItem = [menu itemAtIndex:(syncItemIndex + 1)];
36      if ([menuItem isSeparatorItem]) {
37        followingSeparator = menuItem;
38      }
39    }
40  }
41
42  // TODO(akalin): consolidate this code with the equivalent Windows code in
43  // chrome/browser/ui/views/toolbar_view.cc.
44  int titleId;
45  switch (status) {
46    case sync_ui_util::SYNCED:
47      titleId = IDS_SYNC_MENU_SYNCED_LABEL;
48      break;
49    case sync_ui_util::SYNC_ERROR:
50      titleId = IDS_SYNC_MENU_SYNC_ERROR_LABEL;
51      break;
52    case sync_ui_util::PRE_SYNCED:
53      titleId = IDS_SYNC_START_SYNC_BUTTON_LABEL;
54      break;
55    default:
56      NOTREACHED();
57      // Needed to prevent release-mode warnings.
58      titleId = IDS_SYNC_START_SYNC_BUTTON_LABEL;
59      break;
60  }
61  NSString* title = l10n_util::GetNSStringWithFixup(titleId);
62  [syncMenuItem setTitle:title];
63
64  // If we don't have a sync service, hide any sync-related menu
65  // items.  However, sync_menu_item is enabled/disabled outside of this
66  // function so we don't touch it here, and separators are always disabled.
67  [syncMenuItem setHidden:!syncEnabled];
68  [followingSeparator setHidden:!syncEnabled];
69}
70
71}  // namespace sync_ui_util
72