mod.rs 4.49 KB
Newer Older
1
extern crate gtk;
2
extern crate gettextrs;
3

4
use std::sync::mpsc::Sender;
5
use std::collections::HashMap;
6

7
use gio::ApplicationExt;
8
use self::gtk::prelude::*;
9
use self::gettextrs::gettext;
10

11
use backend::BKCommand;
12
use backend;
13

14 15
use types::Member;
use types::Message;
16
use types::Room;
17
use types::RoomList;
18
use types::StickerGroup;
19

20 21
use passwd::PasswordStorage;

22
use widgets;
23
use cache;
24
use uibuilder;
25

26
use app::InternalCommand;
27

28 29 30 31 32 33 34 35 36 37 38 39 40 41
mod login;
mod sync;
mod user;
mod notifications;
mod state;
mod room;
mod message;
mod directory;
mod notify;
mod attach;
mod member;
mod invite;
mod about;
mod start_chat;
42
mod stickers;
43 44 45 46 47 48 49

pub use self::state::AppState;
use self::message::TmpMsg;
pub use self::message::MsgPos;
pub use self::message::LastViewed;
pub use self::room::RoomPanel;
use self::member::SearchType;
50

51

52
pub struct AppOp {
53
    pub ui: uibuilder::UI,
54
    pub gtk_app: gtk::Application,
55
    pub backend: Sender<backend::BKCommand>,
56
    pub internal: Sender<InternalCommand>,
57 58 59

    pub syncing: bool,
    tmp_msgs: Vec<TmpMsg>,
60
    shown_messages: usize,
61
    pub last_viewed_messages: HashMap<String, Message>,
62

63 64
    pub username: Option<String>,
    pub uid: Option<String>,
65
    pub avatar: Option<String>,
66
    pub server_url: String,
67

68
    pub autoscroll: bool,
69
    pub active_room: Option<String>,
70
    pub rooms: RoomList,
71
    pub roomlist: widgets::RoomList,
72
    pub load_more_spn: gtk::Spinner,
73
    pub more_members_btn: gtk::Button,
74
    pub unsent_messages: HashMap<String, (String, i32)>,
75

76 77 78 79 80
    pub highlighted_entry: Vec<String>,
    pub popover_position: Option<i32>,
    pub popover_search: Option<String>,
    pub popover_closing: bool,

81
    pub state: AppState,
82
    pub since: Option<String>,
83
    pub member_limit: usize,
84 85

    pub logged_in: bool,
86
    pub loading_more: bool,
87 88

    pub invitation_roomid: Option<String>,
Christopher Davis's avatar
Christopher Davis committed
89
    pub md_enabled: bool,
90 91
    invite_list: Vec<Member>,
    search_type: SearchType,
92 93

    pub stickers: Vec<StickerGroup>,
94 95
}

96 97
impl PasswordStorage for AppOp {}

98

99
impl AppOp {
100
    pub fn new(app: gtk::Application,
101
               ui: uibuilder::UI,
102 103
               tx: Sender<BKCommand>,
               itx: Sender<InternalCommand>) -> AppOp {
104
        AppOp {
105
            ui: ui,
106
            gtk_app: app,
107
            load_more_spn: gtk::Spinner::new(),
108
            more_members_btn: gtk::Button::new_with_label(gettext("Load more members").as_str()),
109
            backend: tx,
110
            internal: itx,
111
            autoscroll: true,
112
            active_room: None,
113
            rooms: HashMap::new(),
114 115
            username: None,
            uid: None,
116
            avatar: None,
117
            server_url: String::from("https://matrix.org"),
118 119
            syncing: false,
            tmp_msgs: vec![],
120
            shown_messages: 0,
121
            last_viewed_messages: HashMap::new(),
122
            state: AppState::Login,
123
            roomlist: widgets::RoomList::new(None),
124
            since: None,
125
            member_limit: 50,
126
            unsent_messages: HashMap::new(),
127

128 129 130 131 132
            highlighted_entry: vec![],
            popover_position: None,
            popover_search: None,
            popover_closing: false,

133
            logged_in: false,
134
            loading_more: false,
135

Christopher Davis's avatar
Christopher Davis committed
136
            md_enabled: false,
137
            invitation_roomid: None,
138
            invite_list: vec![],
139
            search_type: SearchType::Invite,
140
            stickers: vec![],
141 142 143
        }
    }

144
    pub fn init(&mut self) {
145 146
        self.set_state(AppState::Loading);

147 148
        if let Ok(data) = cache::load() {
            let r: Vec<Room> = data.rooms.values().cloned().collect();
149
            self.set_rooms(&r, None);
150
            self.last_viewed_messages = data.last_viewed_messages;
151 152 153
            self.since = Some(data.since);
            self.username = Some(data.username);
            self.uid = Some(data.uid);
154 155
        }

156
        if let Ok(pass) = self.get_pass() {
157 158 159
            if let Ok((token, uid)) = self.get_token() {
                self.set_token(Some(token), Some(uid), Some(pass.2));
            } else {
160
                self.set_login_pass(&pass.0, &pass.1, &pass.2);
161 162
                self.connect(Some(pass.0), Some(pass.1), Some(pass.2));
            }
163
        } else {
164
            self.set_state(AppState::Login);
165 166
        }
    }
167

168 169 170 171 172 173
    pub fn activate(&self) {
        let window: gtk::Window = self.ui.builder
            .get_object("main_window")
            .expect("Couldn't find main_window in ui file.");
        window.show();
        window.present();
174 175
    }

176
    pub fn quit(&self) {
177 178 179 180
        self.cache_rooms();
        self.disconnect();
        self.gtk_app.quit();
    }
181
}