1 // Copyright (C) 2010-2015 Petr Pavlu <setup@dagobah.cz>
3 // This file is part of CenterIM.
5 // CenterIM is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
10 // CenterIM is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with CenterIM. If not, see <http://www.gnu.org/licenses/>.
26 Header
*Header::my_instance_
= nullptr;
28 Header
*Header::instance()
33 void Header::onScreenResized()
35 moveResizeRect(CENTERIM
->getScreenArea(CenterIM::HEADER_AREA
));
38 Header::Header() : Window(0, 0, 80, 1, TYPE_NON_FOCUSABLE
, false)
40 setColorScheme(CenterIM::SCHEME_HEADER
);
42 container_
= new CppConsUI::HorizontalListBox(AUTOSIZE
, AUTOSIZE
);
43 addWidget(*container_
, 0, 0);
45 // CenterIM name will always be top-left so we can "forget about" it.
46 char *cimname
= g_strdup_printf("CENTERIM %s", CenterIM::version_
);
47 container_
->appendWidget(*(new CppConsUI::Label(cimname
)));
50 // Pending request indicator.
51 request_indicator_
= new CppConsUI::Label(0, 1, "");
52 request_indicator_
->setColorScheme(CenterIM::SCHEME_HEADER_REQUEST
);
53 container_
->appendWidget(*request_indicator_
);
55 for (GList
*i
= purple_accounts_get_all(); i
!= nullptr; i
= i
->next
) {
56 PurpleAccount
*account
= reinterpret_cast<PurpleAccount
*>(i
->data
);
57 if (purple_account_get_enabled(account
, PACKAGE_NAME
))
58 protocol_count_
.insert(purple_account_get_protocol_id(account
));
61 // Connect to the Accounts singleton.
62 g_assert(ACCOUNTS
!= nullptr);
63 ACCOUNTS
->signal_request_count_change
.connect(
64 sigc::mem_fun(this, &Header::onRequestCountChange
));
66 void *handle
= purple_accounts_get_handle();
67 purple_signal_connect(handle
, "account-signed-on", this,
68 PURPLE_CALLBACK(account_signed_on_
), this);
69 purple_signal_connect(handle
, "account-signed-off", this,
70 PURPLE_CALLBACK(account_signed_off_
), this);
71 purple_signal_connect(handle
, "account-status-changed", this,
72 PURPLE_CALLBACK(account_status_changed_
), this);
73 purple_signal_connect(handle
, "account-alias-changed", this,
74 PURPLE_CALLBACK(account_alias_changed_
), this);
75 purple_signal_connect(
76 handle
, "account-enabled", this, PURPLE_CALLBACK(account_enabled_
), this);
77 purple_signal_connect(
78 handle
, "account-disabled", this, PURPLE_CALLBACK(account_disabled_
), this);
85 purple_signals_disconnect_by_handle(this);
90 g_assert(my_instance_
== nullptr);
92 my_instance_
= new Header
;
96 void Header::finalize()
98 g_assert(my_instance_
!= nullptr);
101 my_instance_
= nullptr;
104 void Header::onRequestCountChange(
105 Accounts
& /*accounts*/, std::size_t request_count
)
107 if (request_count
> 0) {
108 request_indicator_
->setText("* ");
109 request_indicator_
->setWidth(2);
112 request_indicator_
->setText("");
113 request_indicator_
->setWidth(0);
117 void Header::account_signed_on(PurpleAccount
*account
)
119 g_return_if_fail(account
!= nullptr);
121 if (statuses_
.find(account
) != statuses_
.end())
124 auto label
= new CppConsUI::Label(0, 1, "");
125 statuses_
[account
] = label
;
126 container_
->appendWidget(*label
);
128 account_status_changed(
129 account
, nullptr, purple_account_get_active_status(account
));
132 void Header::account_signed_off(PurpleAccount
*account
)
134 g_return_if_fail(account
!= nullptr);
136 if (statuses_
.find(account
) == statuses_
.end())
139 container_
->removeWidget(*statuses_
[account
]);
140 statuses_
.erase(account
);
143 void Header::account_status_changed(
144 PurpleAccount
*account
, PurpleStatus
* /*old*/, PurpleStatus
*cur
)
146 g_return_if_fail(account
!= nullptr);
147 g_return_if_fail(cur
!= nullptr);
149 if (statuses_
.find(account
) == statuses_
.end())
152 const char *prid
= purple_account_get_protocol_id(account
);
153 bool short_text
= protocol_count_
.count(prid
) == 1;
156 CppConsUI::Label
*label
= statuses_
[account
];
159 text
= g_strdup_printf(" %s [%s]", Utils::getStatusIndicator(cur
),
160 purple_account_get_protocol_name(account
));
162 text
= g_strdup_printf(" %s [%s|%s]", Utils::getStatusIndicator(cur
),
163 purple_account_get_protocol_name(account
),
164 purple_account_get_username(account
));
165 label
->setText(text
);
166 label
->setWidth(CppConsUI::Curses::onScreenWidth(text
));
170 void Header::account_alias_changed(PurpleAccount
*account
, const char * /*old*/)
172 g_return_if_fail(account
!= nullptr);
174 account_status_changed(
175 account
, nullptr, purple_account_get_active_status(account
));
178 void Header::account_enabled(PurpleAccount
*account
)
180 g_return_if_fail(account
!= nullptr);
182 const char *prid
= purple_account_get_protocol_id(account
);
183 protocol_count_
.insert(prid
);
185 if (protocol_count_
.count(prid
) == 2)
186 for (Statuses::value_type
&status
: statuses_
) {
187 PurpleAccount
*acc
= status
.first
;
188 if (std::strcmp(purple_account_get_protocol_id(acc
), prid
) == 0)
189 account_status_changed(
190 acc
, nullptr, purple_account_get_active_status(acc
));
194 void Header::account_disabled(PurpleAccount
*account
)
196 g_return_if_fail(account
!= nullptr);
198 const char *prid
= purple_account_get_protocol_id(account
);
199 ProtocolCount::iterator i
= protocol_count_
.find(prid
);
200 protocol_count_
.erase(i
);
202 if (protocol_count_
.count(prid
) == 1)
203 for (Statuses::value_type
&status
: statuses_
) {
204 PurpleAccount
*acc
= status
.first
;
205 if (std::strcmp(purple_account_get_protocol_id(acc
), prid
) == 0)
206 account_status_changed(
207 acc
, nullptr, purple_account_get_active_status(acc
));
211 // vim: set tabstop=2 shiftwidth=2 textwidth=80 expandtab: