aboutsummaryrefslogtreecommitdiff
path: root/src/message/signalproxy/objects/bufferview.rs
blob: 88ae56e8f35c2612cdbba032b4516e18256f2062 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use std::{collections::HashMap, convert::TryFrom, convert::TryInto};

use libquassel_derive::{NetworkList, NetworkMap};

use crate::message::signalproxy::translation::Network;
use crate::message::{StatefulSyncableClient, StatefulSyncableServer, SyncProxy, Syncable};
use crate::primitive::{Variant, VariantList};

#[derive(Debug, Clone, PartialEq)]
// #[network(repr = "list")]
pub struct BufferViewManager {
    // #[network(rename = "bufferViewConfigs", network, variant = "VariantMap")]
    pub buffer_view_configs: HashMap<i32, BufferViewConfig>,
    // // C->S calls

    // requestCreateBufferView(properties: QVariantMap)
    // requestCreateBufferViews(properties: QVariantList)
    // requestDeleteBufferView(bufferViewId: Int)
    // requestDeleteBufferViews(bufferViews: QVariantList)

    // // S->C calls

    // addBufferViewConfig(bufferViewConfigId: Int)
    // deleteBufferViewConfig(bufferViewConfigId: Int)
    // newBufferViewConfig(bufferViewConfigId: Int)
    // /**
    //  * Replaces all properties of the object with the content of the
    //  * "properties" parameter. This parameter is in network representation.
    //  */
    // update(properties: QVariantMap)
}

impl super::NetworkList for BufferViewManager {
    fn to_network_list(&self) -> VariantList {
        let mut res = Vec::with_capacity(2);

        res.push(Variant::ByteArray(s!("bufferViewIds")));
        res.push(Variant::VariantList(
            self.buffer_view_configs
                .iter()
                .map(|(k, _)| i32::try_into(*k).unwrap())
                .collect(),
        ));

        return res;
    }

    fn from_network_list(_input: &mut VariantList) -> Self {
        // TODO Somehow do the initrequests for all the IDs we get here
        Self {
            buffer_view_configs: HashMap::new(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, NetworkList, NetworkMap)]
pub struct BufferViewConfig {
    #[network(rename = "BufferList", network, variant = "VariantList")]
    pub buffers: Vec<i32>,
    #[network(rename = "RemovedBuffers", network, variant = "VariantList")]
    pub removed_buffers: Vec<i32>,
    #[network(rename = "TemporarilyRemovedBuffers", network, variant = "VariantList")]
    pub temporarily_removed_buffers: Vec<i32>,

    // TODO think about how to handle the buffer view id
    //   we might introduce a default flag for the network macro
    // #[network(rename = "bufferViewId")]
    // pub buffer_view_id: i32,
    #[network(rename = "bufferViewName")]
    pub buffer_view_name: String,
    #[network(rename = "networkId")]
    pub network_id: i32,
    #[network(rename = "addNewBuffersAutomatically")]
    pub add_new_buffers_automatically: bool,
    #[network(rename = "sortAlphabetically")]
    pub sort_alphabetically: bool,
    #[network(rename = "hideInactiveBuffers")]
    pub hide_inactive_buffers: bool,
    #[network(rename = "hideInactiveNetworks")]
    pub hide_inactive_networks: bool,
    #[network(rename = "disableDecoration")]
    pub disable_decoration: bool,
    #[network(rename = "allowedBufferTypes")]
    pub allowed_buffers_types: i32,
    #[network(rename = "minimumActivity")]
    pub minimum_activity: i32,
    #[network(rename = "showSearch")]
    pub show_search: bool,
}

#[allow(dead_code)]
impl BufferViewConfig {
    // TODO add sync to all functions
    // TODO requestAddBuffer(bufferId: BufferId, pos: Int)
    // TODO requestMoveBuffer(bufferId: BufferId, pos: Int)
    // TODO requestRemoveBuffer(bufferId: BufferId)
    // TODO requestRemoveBufferPermanently(bufferId: BufferId)
    // TODO requestSetBufferViewName(bufferViewName: QString)
    // /**
    //  * Replaces all properties of the object with the content of the
    //  * "properties" parameter. This parameter is in network representation.
    //  */
    // DONE requestUpdate(properties: QVariantMap)

    // // S->C calls
    // DONE addBuffer(bufferId: BufferId, pos: Int)
    // DONE moveBuffer(bufferId: BufferId, pos: Int)
    // DONE removeBuffer(bufferId: BufferId)
    // DONE removeBufferPermanently(bufferId: BufferId)
    // TODO setAddNewBuffersAutomatically(addNewBuffersAutomatically: Bool)
    // TODO setAllowedBufferTypes(bufferTypes: Int)
    // TODO setBufferViewName(bufferViewName: QString)
    // TODO setDisableDecoration(disableDecoration: Bool)
    // TODO setHideInactiveBuffers(hideInactiveBuffers: Bool)
    // TODO setHideInactiveNetworks(hideInactiveNetworks: Bool)
    // TODO setMinimumActivity(activity: Int)
    // TODO setNetworkId(networkId: NetworkId)
    // TODO setShowSearch(showSearch: Bool)
    // TODO setSortAlphabetically(sortAlphabetically: Bool)

    fn add_buffer(&mut self, id: i32, pos: usize) {
        if !self.buffers.contains(&id) {
            self.buffers.insert(pos, id)
        }

        #[cfg(feature = "server")]
        {
            // TODO replace the None with self.buffer_view_id
            self.send_sync(None, "addBuffer", vec![id.into(), (pos as i32).into()])
        }
    }

    fn move_buffer(&mut self, id: i32, pos: usize) {
        let old_pos = self.buffers.iter().position(|&x| x == id).unwrap();
        self.buffers.remove(old_pos);
        self.buffers.insert(pos, id);
    }

    fn remove_buffer(&mut self, id: i32) {
        if self.buffers.contains(&id) {
            let old_pos = self.buffers.iter().position(|&x| x == id).unwrap();
            self.buffers.remove(old_pos);
        }

        if self.removed_buffers.contains(&id) {
            let old_pos = self.removed_buffers.iter().position(|&x| x == id).unwrap();
            self.removed_buffers.remove(old_pos);
        }

        if !self.temporarily_removed_buffers.contains(&id) {
            self.buffers.push(id)
        }
    }

    fn remove_buffer_permanently(&mut self, id: i32) {
        if self.buffers.contains(&id) {
            let old_pos = self.buffers.iter().position(|&x| x == id).unwrap();
            self.buffers.remove(old_pos);
        }

        if self.temporarily_removed_buffers.contains(&id) {
            let old_pos = self.removed_buffers.iter().position(|&x| x == id).unwrap();
            self.removed_buffers.remove(old_pos);
        }

        if !self.removed_buffers.contains(&id) {
            self.buffers.push(id)
        }
    }
}

#[cfg(feature = "client")]
impl StatefulSyncableClient for BufferViewConfig {
    fn sync_custom(&mut self, mut msg: crate::message::SyncMessage)
    where
        Self: Sized,
    {
        log::debug!("entering bufferviewconfig sync_custom()");
        match msg.slot_name.as_str() {
            "addBuffer" => self.add_buffer(
                msg.params.remove(0).try_into().unwrap(),
                i32::try_from(msg.params.remove(0)).unwrap() as usize,
            ),
            "moveBuffer" => self.move_buffer(
                msg.params.remove(0).try_into().unwrap(),
                i32::try_from(msg.params.remove(0)).unwrap() as usize,
            ),
            "removeBuffer" => self.remove_buffer(msg.params.remove(0).try_into().unwrap()),
            "removeBufferPermanently" => {
                self.remove_buffer_permanently(msg.params.remove(0).try_into().unwrap())
            }
            _ => (),
        }
    }
}

#[cfg(feature = "server")]
impl StatefulSyncableServer for BufferViewConfig {
    fn sync_custom(&mut self, mut msg: crate::message::SyncMessage)
    where
        Self: Sized,
    {
        match msg.slot_name.as_str() {
            _ => (),
        }
    }
}

impl Syncable for BufferViewConfig {
    const CLASS: &'static str = "BufferViewConfig";
}