~elementary-apps/pantheon-chat/trunk

« back to all changes in this revision

Viewing changes to src/AccountPopover.vala

  • Committer: Corentin Noël
  • Date: 2014-06-21 21:06:00 UTC
  • Revision ID: corentin@elementaryos.org-20140621210600-s26ajp7z6wugty8k
First Commit.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-
 
2
/*-
 
3
 * Copyright (c) 2013-2014 Pantheon Chat Developers (http://launchpad.net/pantheon-chat)
 
4
 *
 
5
 * This library is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU Library General Public
 
7
 * License as published by the Free Software Foundation; either
 
8
 * version 2 of the License, or (at your option) any later version.
 
9
 *
 
10
 * This library 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 GNU
 
13
 * Library General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU Library General Public
 
16
 * License along with this library; if not, write to the
 
17
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
18
 * Boston, MA 02111-1307, USA.
 
19
 *
 
20
 * Authored by: Corentin Noël <corentin@elementaryos.org>
 
21
 */
 
22
 
 
23
public class Chat.AccountPopover : Gtk.Popover {
 
24
    private Gtk.ListStore model;
 
25
    private Gtk.Label name_label;
 
26
    private Gtk.Label message_label;
 
27
    private Gtk.Button avatar_image;
 
28
 
 
29
    public AccountPopover () {
 
30
        avatar_image = new Gtk.Button.from_icon_name ("avatar-default", Gtk.IconSize.DIALOG);
 
31
        avatar_image.relief = Gtk.ReliefStyle.NONE;
 
32
 
 
33
        name_label = new Gtk.Label ("");
 
34
        name_label.xalign = 0;
 
35
 
 
36
        message_label = new Gtk.Label ("");
 
37
        message_label.use_markup = true;
 
38
        message_label.xalign = 0;
 
39
 
 
40
        var profile_container = new Gtk.Grid ();
 
41
        profile_container.column_spacing = 12;
 
42
        profile_container.row_spacing = 6;
 
43
        profile_container.margin = 12;
 
44
        profile_container.attach (avatar_image, 0, 0, 1, 2);
 
45
        profile_container.attach (name_label, 1, 0, 1, 1);
 
46
        profile_container.attach (message_label, 1, 1, 1, 1);
 
47
 
 
48
        model = new Gtk.ListStore (3, typeof (Gdk.Pixbuf), typeof (string), typeof (TelepathyGLib.Account));
 
49
 
 
50
        var view = new Gtk.IconView.with_model (model);
 
51
        view.activate_on_single_click = true;
 
52
        view.set_pixbuf_column (0);
 
53
        view.set_tooltip_column (1);
 
54
        view.item_activated.connect ((path) => {
 
55
            view.unselect_all ();
 
56
            Gtk.TreeIter iter;
 
57
            model.get_iter (out iter, path);
 
58
            Value val;
 
59
            model.get_value (iter, 2, out val);
 
60
            hide ();
 
61
        });
 
62
 
 
63
        var layout = new Gtk.Grid ();
 
64
        layout.orientation = Gtk.Orientation.VERTICAL;
 
65
        layout.add (profile_container);
 
66
        layout.add (new Gtk.Separator (Gtk.Orientation.HORIZONTAL));
 
67
        layout.add (view);
 
68
        layout.show_all ();
 
69
 
 
70
        add (layout);
 
71
 
 
72
        try {
 
73
            Gtk.TreeIter iter;
 
74
            model.append (out iter);
 
75
            Gdk.Pixbuf pixbuf = Gtk.IconTheme.get_default ().load_icon ("list-add-symbolic", 24, Gtk.IconLookupFlags.GENERIC_FALLBACK);
 
76
            model.set (iter, 0, pixbuf, 1, _("Add Account…"));
 
77
        } catch (Error e) {
 
78
            critical (e.message);
 
79
        }
 
80
 
 
81
        load_accounts ();
 
82
    }
 
83
 
 
84
    private void load_accounts () {
 
85
        var telepathy_manager = TelepathyManager.get_default ();
 
86
        var accounts = telepathy_manager.get_accounts ();
 
87
        foreach (var account in accounts) {
 
88
            add_button (account);
 
89
        }
 
90
 
 
91
        telepathy_manager.account_added.connect ((account) => {add_button (account);});
 
92
        foreach (unowned TelepathyGLib.Contact contact in telepathy_manager.get_myself_list ()) {
 
93
            if (contact.avatar_file != null) {
 
94
                avatar_image.image = new Gtk.Image.from_gicon (new FileIcon (contact.avatar_file), Gtk.IconSize.DIALOG);
 
95
                name_label.label = contact.alias;
 
96
                message_label.label = contact.presence_message;
 
97
            }
 
98
        }
 
99
 
 
100
        telepathy_manager.myself_changed.connect ((contact) => {
 
101
            if (contact.avatar_file != null) {
 
102
                avatar_image.image = new Gtk.Image.from_gicon (new FileIcon (contact.avatar_file), Gtk.IconSize.DIALOG);
 
103
                name_label.label = contact.alias;
 
104
                message_label.label = contact.presence_message;
 
105
            }
 
106
        });
 
107
    }
 
108
 
 
109
    private void add_button (TelepathyGLib.Account account) {
 
110
        try {
 
111
            Gtk.TreeIter iter;
 
112
            model.prepend (out iter);
 
113
            Gdk.Pixbuf pixbuf = Gtk.IconTheme.get_default ().load_icon (account.icon_name, 24, Gtk.IconLookupFlags.GENERIC_FALLBACK);
 
114
            model.set (iter, 0, pixbuf, 1, account.display_name, 2, account);
 
115
        } catch (Error e) {
 
116
            critical (e.message);
 
117
        }
 
118
    }
 
119
}
 
 
b'\\ No newline at end of file'