people_result.cc revision 68043e1e95eeb07d5cae7aca370b26518b0867d6
1// Copyright 2013 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/app_list/search/people/people_result.h"
6
7#include <vector>
8
9#include "base/bind.h"
10#include "base/memory/ref_counted.h"
11#include "base/strings/utf_string_conversions.h"
12#include "chrome/browser/profiles/profile.h"
13#include "chrome/browser/ui/app_list/search/common/url_icon_source.h"
14#include "chrome/browser/ui/browser_navigator.h"
15#include "grit/generated_resources.h"
16#include "grit/theme_resources.h"
17#include "ui/base/l10n/l10n_util.h"
18#include "ui/base/resource/resource_bundle.h"
19
20namespace {
21
22const int kIconSize = 32;
23const char kImageSizePath[] = "s32-p/";
24const char kEmailUrlPrefix[] = "mailto:";
25
26// Add a query parameter to specify the size to fetch the image in. The
27// original profile image can be of an arbitrary size, we ask the server to
28// crop it to a square 32x32 using its smart cropping algorithm.
29GURL GetImageUrl(const GURL& url) {
30  std::string image_filename = url.ExtractFileName();
31  if (image_filename.empty())
32    return url;
33
34  return url.Resolve(kImageSizePath + image_filename);
35}
36
37}  // namespace
38
39namespace app_list {
40
41PeopleResult::PeopleResult(Profile* profile,
42                           const std::string& id,
43                           const std::string& display_name,
44                           const std::string& email,
45                           double interaction_rank,
46                           const GURL& image_url)
47    : profile_(profile),
48      id_(id),
49      display_name_(display_name),
50      email_(email),
51      interaction_rank_(interaction_rank),
52      image_url_(image_url),
53      weak_factory_(this) {
54  set_id(id_);
55  set_title(UTF8ToUTF16(display_name_));
56  set_relevance(interaction_rank_);
57  set_details(UTF8ToUTF16(email_));
58
59  SetDefaultActions();
60
61  image_ = gfx::ImageSkia(
62      new UrlIconSource(base::Bind(&PeopleResult::OnIconLoaded,
63                                   weak_factory_.GetWeakPtr()),
64                        profile_->GetRequestContext(),
65                        GetImageUrl(image_url_),
66                        kIconSize,
67                        IDR_PROFILE_PICTURE_LOADING),
68      gfx::Size(kIconSize, kIconSize));
69  SetIcon(image_);
70}
71
72PeopleResult::~PeopleResult() {
73}
74
75void PeopleResult::Open(int event_flags) {
76  InvokeAction(0, event_flags);
77}
78
79void PeopleResult::InvokeAction(int action_index, int event_flags) {
80  DCHECK_EQ(0, action_index);
81  // Currently we support only one action, sending mail.
82  SendEmail();
83}
84
85scoped_ptr<ChromeSearchResult> PeopleResult::Duplicate() {
86  return scoped_ptr<ChromeSearchResult>(new PeopleResult(profile_, id_,
87                                                         display_name_,
88                                                         email_,
89                                                         interaction_rank_,
90                                                         image_url_)).Pass();
91}
92
93void PeopleResult::OnIconLoaded() {
94  // Remove the existing image reps since the icon data is loaded and they
95  // need to be re-created.
96  const std::vector<gfx::ImageSkiaRep>& image_reps = image_.image_reps();
97  for (size_t i = 0; i < image_reps.size(); ++i)
98    image_.RemoveRepresentation(image_reps[i].scale());
99
100  SetIcon(image_);
101}
102
103void PeopleResult::SetDefaultActions() {
104  Actions actions;
105
106  ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
107  actions.push_back(Action(
108      *bundle.GetImageSkiaNamed(IDR_PEOPLE_SEARCH_ACTION_EMAIL),
109      *bundle.GetImageSkiaNamed(IDR_PEOPLE_SEARCH_ACTION_EMAIL_HOVER),
110      *bundle.GetImageSkiaNamed(IDR_PEOPLE_SEARCH_ACTION_EMAIL_PRESSED),
111      l10n_util::GetStringUTF16(IDS_PEOPLE_SEARCH_ACTION_EMAIL_TOOLTIP)));
112  SetActions(actions);
113}
114
115void PeopleResult::SendEmail() {
116  chrome::NavigateParams params(profile_,
117                                GURL(kEmailUrlPrefix + email_),
118                                content::PAGE_TRANSITION_LINK);
119  // If no window exists, this will open a new window this one tab.
120  params.disposition = NEW_FOREGROUND_TAB;
121  chrome::Navigate(&params);
122}
123
124ChromeSearchResultType PeopleResult::GetType() {
125  return SEARCH_PEOPLE_SEARCH_RESULT;
126}
127
128}  // namespace app_list
129