blob: 4d218c5ebbdf36711ad64a03837c2e28be5ecaee [file] [log] [blame]
joedow7cc2c862016-09-13 16:05:571// Copyright 2016 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
joedow6243fba2016-09-14 18:13:495#include <gtk/gtk.h>
6
7#include <memory>
8#include <string>
9
10#include "base/bind.h"
11#include "base/callback.h"
12#include "base/callback_helpers.h"
13#include "base/i18n/message_formatter.h"
14#include "base/location.h"
15#include "base/logging.h"
16#include "base/macros.h"
joedow6243fba2016-09-14 18:13:4917#include "base/strings/utf_string_conversions.h"
18#include "base/time/time.h"
19#include "base/timer/timer.h"
20#include "remoting/base/string_resources.h"
joedow7cc2c862016-09-13 16:05:5721#include "remoting/host/it2me/it2me_confirmation_dialog.h"
joedow6243fba2016-09-14 18:13:4922#include "ui/base/glib/glib_signal.h"
23#include "ui/base/l10n/l10n_util.h"
joedow7cc2c862016-09-13 16:05:5724
25namespace remoting {
26
joedow6243fba2016-09-14 18:13:4927namespace {
28
29// Time to wait before closing the dialog and cancelling the connection.
30constexpr base::TimeDelta kDialogTimeout = base::TimeDelta::FromMinutes(1);
31
32class It2MeConfirmationDialogLinux : public It2MeConfirmationDialog {
33 public:
34 It2MeConfirmationDialogLinux();
35 ~It2MeConfirmationDialogLinux() override;
36
37 // It2MeConfirmationDialog implementation.
38 void Show(const std::string& remote_user_email,
39 const ResultCallback& callback) override;
40
41 private:
42 // Creates a dialog window and makes it visible.
43 void CreateWindow(const std::string& remote_user_email);
44
45 // Destroys the dialog window (if created) and stop |dialog_timer_|.
46 void Hide();
47
48 // Handles user input from the dialog.
49 CHROMEG_CALLBACK_1(It2MeConfirmationDialogLinux, void, OnResponse, GtkDialog*,
50 int);
51
52 GtkWidget* confirmation_window_ = nullptr;
53
54 ResultCallback result_callback_;
55
56 base::OneShotTimer dialog_timer_;
57
58 DISALLOW_COPY_AND_ASSIGN(It2MeConfirmationDialogLinux);
59};
60
61It2MeConfirmationDialogLinux::It2MeConfirmationDialogLinux() {}
62
63It2MeConfirmationDialogLinux::~It2MeConfirmationDialogLinux() {
64 Hide();
65}
66
67void It2MeConfirmationDialogLinux::Show(const std::string& remote_user_email,
68 const ResultCallback& callback) {
69 DCHECK(!remote_user_email.empty());
70 DCHECK(callback);
71 DCHECK(!result_callback_);
72
73 result_callback_ = callback;
74
75 CreateWindow(remote_user_email);
76
77 dialog_timer_.Start(FROM_HERE, kDialogTimeout,
78 base::Bind(&It2MeConfirmationDialogLinux::OnResponse,
79 base::Unretained(this),
80 /*dialog=*/nullptr, GTK_RESPONSE_NONE));
81}
82
83void It2MeConfirmationDialogLinux::Hide() {
84 dialog_timer_.Stop();
85
86 if (confirmation_window_) {
87 gtk_widget_destroy(confirmation_window_);
88 confirmation_window_ = nullptr;
89 }
90}
91
92void It2MeConfirmationDialogLinux::CreateWindow(
93 const std::string& remote_user_email) {
94 DCHECK(!confirmation_window_);
95
96 confirmation_window_ = gtk_dialog_new_with_buttons(
97 l10n_util::GetStringUTF8(IDS_PRODUCT_NAME).c_str(),
98 /*parent=*/nullptr,
99 static_cast<GtkDialogFlags>(GTK_DIALOG_MODAL),
100 l10n_util::GetStringUTF8(IDS_SHARE_CONFIRM_DIALOG_DECLINE).c_str(),
101 GTK_RESPONSE_CANCEL,
102 l10n_util::GetStringUTF8(IDS_SHARE_CONFIRM_DIALOG_CONFIRM).c_str(),
103 GTK_RESPONSE_OK,
104 /*next_button=*/nullptr);
105
106 gtk_dialog_set_default_response(GTK_DIALOG(confirmation_window_),
107 GTK_RESPONSE_CANCEL);
108
109 gtk_window_set_resizable(GTK_WINDOW(confirmation_window_), false);
110
111 gtk_window_set_keep_above(GTK_WINDOW(confirmation_window_), true);
112
113 g_signal_connect(confirmation_window_, "response",
114 G_CALLBACK(OnResponseThunk), this);
115
116 GtkWidget* content_area =
117 gtk_dialog_get_content_area(GTK_DIALOG(confirmation_window_));
118
119 base::string16 dialog_text =
120 base::i18n::MessageFormatter::FormatWithNumberedArgs(
121 l10n_util::GetStringUTF16(
122 IDS_SHARE_CONFIRM_DIALOG_MESSAGE_WITH_USERNAME),
123 remote_user_email);
124 GtkWidget* text_label = gtk_label_new(base::UTF16ToUTF8(dialog_text).c_str());
125 gtk_label_set_line_wrap(GTK_LABEL(text_label), true);
thomasandersonac0b0fc2017-03-11 03:15:04126 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
joedow6243fba2016-09-14 18:13:49127 gtk_misc_set_padding(GTK_MISC(text_label), 12, 12);
thomasandersonac0b0fc2017-03-11 03:15:04128 G_GNUC_END_IGNORE_DEPRECATIONS;
joedow6243fba2016-09-14 18:13:49129
130 gtk_container_add(GTK_CONTAINER(content_area), text_label);
131 gtk_widget_show_all(content_area);
132
133 gtk_window_set_urgency_hint(GTK_WINDOW(confirmation_window_), true);
134 gtk_window_present(GTK_WINDOW(confirmation_window_));
135}
136
137void It2MeConfirmationDialogLinux::OnResponse(GtkDialog* dialog,
138 int response_id) {
139 DCHECK(result_callback_);
140
141 Hide();
142 base::ResetAndReturn(&result_callback_).Run(
143 (response_id == GTK_RESPONSE_OK) ? Result::OK : Result::CANCEL);
144}
145
146} // namespace
147
joedowd15dc6a2017-03-20 16:52:26148std::unique_ptr<It2MeConfirmationDialog>
149It2MeConfirmationDialogFactory::Create() {
Jinho Bang138fde32018-01-18 23:13:42150 return std::make_unique<It2MeConfirmationDialogLinux>();
joedow7cc2c862016-09-13 16:05:57151}
152
153} // namespace remoting