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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
use std::process::Command;
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::{Duration, Instant};

use crossbeam_channel::Sender;
use dbus::arg;
use dbus::blocking::{stdintf::org_freedesktop_dbus::Properties, Connection};
use dbus::Message;
use serde_derive::Deserialize;

use crate::blocks::{Block, ConfigBlock, Update};
use crate::config::SharedConfig;
use crate::errors::*;
use crate::formatting::value::Value;
use crate::formatting::FormatTemplate;
use crate::scheduler::Task;
use crate::util::battery_level_to_icon;
use crate::widgets::text::TextWidget;
use crate::widgets::{I3BarWidget, State};

pub struct KDEConnect {
    id: usize,
    device_id: String,
    device_name: Arc<Mutex<String>>,
    battery_charge: Arc<Mutex<i32>>,
    battery_state: Arc<Mutex<bool>>,
    notif_count: Arc<Mutex<i32>>,
    phone_reachable: Arc<Mutex<bool>>,
    // TODO
    //notif_text: Arc<Mutex<String>>,
    bat_good: i32,
    bat_info: i32,
    bat_warning: i32,
    bat_critical: i32,
    format: FormatTemplate,
    format_disconnected: FormatTemplate,
    output: TextWidget,
    shared_config: SharedConfig,
}

#[derive(Deserialize, Debug, Clone)]
#[serde(deny_unknown_fields, default)]
pub struct KDEConnectConfig {
    pub device_id: Option<String>,

    /// The threshold above which the remaining capacity is shown as good
    pub bat_good: i32,

    /// The threshold below which the remaining capacity is shown as info
    pub bat_info: i32,

    /// The threshold below which the remaining capacity is shown as warning
    pub bat_warning: i32,

    /// The threshold below which the remaining capacity is shown as critical
    pub bat_critical: i32,

    /// Format string for displaying phone information.
    pub format: String,

    /// Format string for displaying phone information when it is disconnected.
    pub format_disconnected: String,
}

impl Default for KDEConnectConfig {
    fn default() -> Self {
        Self {
            device_id: None,
            bat_good: 60,
            bat_info: 60,
            bat_warning: 30,
            bat_critical: 15,
            format: "{name} {bat_icon}{bat_charge} {notif_icon}{notif_count}".to_string(),
            format_disconnected: "{name}".to_string(),
        }
    }
}

impl ConfigBlock for KDEConnect {
    type Config = KDEConnectConfig;

    fn new(
        id: usize,
        block_config: Self::Config,
        shared_config: SharedConfig,
        send: Sender<Task>,
    ) -> Result<Self> {
        let send2 = send.clone();
        let send3 = send.clone();
        let send4 = send.clone();
        let send5 = send.clone();
        let send6 = send.clone();
        let send7 = send.clone();

        let c = Connection::new_session().block_error(
            "kdeconnect",
            &"Failed to establish D-Bus connection".to_string(),
        )?;

        let device_id = if block_config.device_id.is_none() {
            // If none specified in block config, just grab the first device found.
            let p1 = c.with_proxy(
                "org.kde.kdeconnect",
                "/modules/kdeconnect",
                Duration::from_millis(5000),
            );
            // method call opts: only_reachable=false, only_paired=true
            let (devices,): (Vec<String>,) = p1
                .method_call("org.kde.kdeconnect.daemon", "devices", (false, true))
                .block_error(
                    "kdeconnect",
                    &"Couldn't connect to KDE Connect daemon".to_string(),
                )?;
            if devices.is_empty() {
                return Err(BlockError(
                    "kdeconnect".to_owned(),
                    "No devices found.".to_owned(),
                ));
            }
            devices[0].clone()
        } else {
            block_config.device_id.unwrap()
        };

        let p2 = c.with_proxy(
            "org.kde.kdeconnect",
            format!("/modules/kdeconnect/devices/{}", device_id),
            Duration::from_millis(5000),
        );
        let initial_name: String = p2
            .get("org.kde.kdeconnect.device", "name")
            .unwrap_or_else(|_| String::from(""));
        let initial_reachable: bool = p2
            .get("org.kde.kdeconnect.device", "isReachable")
            .unwrap_or(false);

        // Test whether we are dealing with kdeconnect v20.08.03 or older,
        // or kdeconnect v20.11.80 or newer, so we can adapt to the differences.
        //
        // Starting with kdeconnect v20.11.80, the version output by the cli
        // matches the versioning scheme used by Ubuntu, where as before that it
        // was  1.3.x or 1.4.x.
        let old_kdeconnect = Command::new("kdeconnect-cli")
            .args(&["--version"])
            .output()
            .block_error(
                "kdeconnect",
                "Failed to check kdeconnect version. Is it installed?",
            )
            .and_then(|raw_output| {
                String::from_utf8(raw_output.stdout)
                    .block_error("kdeconnect", "Failed to check kdeconnect version.")
            })
            .unwrap()
            .contains("kdeconnect-cli 1.");

        let initial_charge = if old_kdeconnect {
            let (charge,): (i32,) = p2
                .method_call("org.kde.kdeconnect.device.battery", "charge", ())
                .unwrap_or((0,));
            charge
        } else {
            let p3 = c.with_proxy(
                "org.kde.kdeconnect",
                format!("/modules/kdeconnect/devices/{}/battery", device_id),
                Duration::from_millis(5000),
            );
            let charge: i32 = p3
                .get("org.kde.kdeconnect.device.battery", "charge")
                .unwrap_or(0);
            charge
        };

        let initial_charging = if old_kdeconnect {
            let (charging,): (bool,) = p2
                .method_call("org.kde.kdeconnect.device.battery", "isCharging", ())
                .unwrap_or((false,));
            charging
        } else {
            let p3 = c.with_proxy(
                "org.kde.kdeconnect",
                format!("/modules/kdeconnect/devices/{}/battery", device_id),
                Duration::from_millis(5000),
            );
            let charging: bool = p3
                .get("org.kde.kdeconnect.device.battery", "isCharging")
                .unwrap_or(false);
            charging
        };

        let initial_notifications = if old_kdeconnect {
            let (notifications,): (Vec<String>,) = p2
                .method_call(
                    "org.kde.kdeconnect.device.notifications",
                    "activeNotifications",
                    (),
                )
                .unwrap_or((vec![String::from("")],));
            notifications
        } else {
            let p4 = c.with_proxy(
                "org.kde.kdeconnect",
                format!("/modules/kdeconnect/devices/{}/notifications", device_id),
                Duration::from_millis(5000),
            );
            let (notifications,): (Vec<String>,) = p4
                .method_call(
                    "org.kde.kdeconnect.device.notifications",
                    "activeNotifications",
                    (),
                )
                .unwrap_or((vec![String::from("")],));
            notifications
        };

        let device_id_copy = device_id.clone();
        let device_name = Arc::new(Mutex::new(initial_name));
        let device_name_copy = device_name.clone();
        let charge = Arc::new(Mutex::new(initial_charge));
        let charge_copy = charge.clone();
        // TODO: revisit this lint
        #[allow(clippy::mutex_atomic)]
        let charging = Arc::new(Mutex::new(initial_charging));
        let charging_copy = charging.clone();
        let notif_count = Arc::new(Mutex::new(initial_notifications.len() as i32));
        let notif_count_copy1 = notif_count.clone();
        let notif_count_copy2 = notif_count.clone();
        let notif_count_copy3 = notif_count.clone();
        // TODO: revisit this lint
        #[allow(clippy::mutex_atomic)]
        let reachable = Arc::new(Mutex::new(initial_reachable));
        let reachable_copy1 = reachable.clone();
        let reachable_copy2 = reachable.clone();

        // TODO: See if can reliably get the text and/or app of the most recent notification.
        // Will need to see if the order of notifications is guaranteed or not.
        // Also, need to call activeNotifications each time a notification is added/removed/updated,
        // because the signal only gives us a 'public_id' and no other useful info
        //let last_notif_text = if initial_notifications.get(0).is_none() {
        //    Arc::new(Mutex::new("".to_string()))
        //} else {
        //    Arc::new(Mutex::new(initial_notifications.get(0).unwrap().to_string()))
        //};

        thread::Builder::new()
            .name("kdeconnect".into())
            .spawn(move || {
                let c = Connection::new_session()
                    .expect("Failed to establish D-Bus connection in thread");

                let p1 = c.with_proxy(
                    "org.kde.kdeconnect",
                    format!("/modules/kdeconnect/devices/{}", device_id_copy),
                    Duration::from_millis(5000),
                );

                let _device_name_handler = p1.match_signal(
                    move |s: OrgKdeKdeconnectDeviceNameChanged, _: &Connection, _: &Message| {
                        let mut name = device_name_copy.lock().unwrap();
                        *name = s.name;

                        // Tell block to update now.
                        send2
                            .send(Task {
                                id,
                                update_time: Instant::now(),
                            })
                            .unwrap();

                        true
                    },
                );

                let _phone_reachable_handler = p1.match_signal(
                    move |s: OrgKdeKdeconnectDeviceReachableChanged,
                          _: &Connection,
                          _: &Message| {
                        let mut reachable = reachable_copy1.lock().unwrap();
                        *reachable = s.reachable;

                        // Tell block to update now.
                        // KDEConnect emits both stateChanged and chargeChanged
                        // whenever there is an update regardless of whether or
                        // not they both changed. So we only need to send updates
                        // in one of the two battery signal handlers. Hopefully
                        // one day they add proper PropertiesChanged signals.
                        send6
                            .send(Task {
                                id,
                                update_time: Instant::now(),
                            })
                            .unwrap();

                        true
                    },
                );

                if old_kdeconnect {
                    let _battery_state_handler = p1.match_signal(
                        move |s: OrgKdeKdeconnectDeviceBatteryStateChanged,
                              _: &Connection,
                              _: &Message| {
                            let mut charging = charging_copy.lock().unwrap();
                            *charging = s.charging;

                            // Tell block to update now.
                            // KDEConnect emits both stateChanged and chargeChanged
                            // whenever there is an update regardless of whether or
                            // not they both changed. So we only need to send updates
                            // in one of the two battery signal handlers. Hopefully
                            // one day they add proper PropertiesChanged signals.
                            send.send(Task {
                                id,
                                update_time: Instant::now(),
                            })
                            .unwrap();

                            true
                        },
                    );

                    let _battery_charge_handler = p1.match_signal(
                        move |s: OrgKdeKdeconnectDeviceBatteryChargeChanged,
                              _: &Connection,
                              _: &Message| {
                            let mut charge = charge_copy.lock().unwrap();
                            *charge = s.charge;

                            true
                        },
                    );
                } else {
                    let p2 = c.with_proxy(
                        "org.kde.kdeconnect",
                        format!("/modules/kdeconnect/devices/{}/battery", device_id_copy),
                        Duration::from_millis(5000),
                    );
                    let _battery_state_handler = p2.match_signal(
                        move |s: OrgKdeKdeconnectDeviceBatteryRefreshed,
                              _: &Connection,
                              _: &Message| {
                            let mut charging = charging_copy.lock().unwrap();
                            *charging = s.is_charging;

                            let mut charge = charge_copy.lock().unwrap();
                            *charge = s.charge;

                            send.send(Task {
                                id,
                                update_time: Instant::now(),
                            })
                            .unwrap();

                            true
                        },
                    );
                };

                if old_kdeconnect {
                    let _notification_added_handler = p1.match_signal(
                        move |_s: OrgKdeKdeconnectDeviceNotificationsNotificationPosted,
                              _: &Connection,
                              _: &Message| {
                            let mut notif_count = notif_count_copy1.lock().unwrap();
                            *notif_count += 1;

                            // Tell block to update now.
                            send3
                                .send(Task {
                                    id,
                                    update_time: Instant::now(),
                                })
                                .unwrap();

                            true
                        },
                    );

                    let _notification_removed_handler = p1.match_signal(
                        move |_s: OrgKdeKdeconnectDeviceNotificationsNotificationRemoved,
                              _: &Connection,
                              _: &Message| {
                            let mut notif_count = notif_count_copy2.lock().unwrap();
                            *notif_count = if *notif_count - 1 < 0 {
                                0
                            } else {
                                *notif_count - 1
                            };

                            // Tell block to update now.
                            send4
                                .send(Task {
                                    id,
                                    update_time: Instant::now(),
                                })
                                .unwrap();

                            true
                        },
                    );

                    let _notification_all_removed_handler = p1.match_signal(
                        move |_s: OrgKdeKdeconnectDeviceNotificationsAllNotificationsRemoved,
                              _: &Connection,
                              _: &Message| {
                            let mut notif_count = notif_count_copy3.lock().unwrap();
                            *notif_count = 0;

                            // Tell block to update now.
                            send5
                                .send(Task {
                                    id,
                                    update_time: Instant::now(),
                                })
                                .unwrap();

                            true
                        },
                    );
                } else {
                    let p3 = c.with_proxy(
                        "org.kde.kdeconnect",
                        format!(
                            "/modules/kdeconnect/devices/{}/notifications",
                            device_id_copy
                        ),
                        Duration::from_millis(5000),
                    );

                    let _notification_added_handler = p3.match_signal(
                        move |_s: OrgKdeKdeconnectDeviceNotificationsNotificationPosted,
                              _: &Connection,
                              _: &Message| {
                            let mut notif_count = notif_count_copy1.lock().unwrap();
                            *notif_count += 1;

                            // Tell block to update now.
                            send3
                                .send(Task {
                                    id,
                                    update_time: Instant::now(),
                                })
                                .unwrap();

                            true
                        },
                    );

                    let _notification_removed_handler = p3.match_signal(
                        move |_s: OrgKdeKdeconnectDeviceNotificationsNotificationRemoved,
                              _: &Connection,
                              _: &Message| {
                            let mut notif_count = notif_count_copy2.lock().unwrap();
                            *notif_count = if *notif_count - 1 < 0 {
                                0
                            } else {
                                *notif_count - 1
                            };

                            // Tell block to update now.
                            send4
                                .send(Task {
                                    id,
                                    update_time: Instant::now(),
                                })
                                .unwrap();

                            true
                        },
                    );

                    let _notification_all_removed_handler = p3.match_signal(
                        move |_s: OrgKdeKdeconnectDeviceNotificationsAllNotificationsRemoved,
                              _: &Connection,
                              _: &Message| {
                            let mut notif_count = notif_count_copy3.lock().unwrap();
                            *notif_count = 0;

                            // Tell block to update now.
                            send5
                                .send(Task {
                                    id,
                                    update_time: Instant::now(),
                                })
                                .unwrap();

                            true
                        },
                    );

                    //if notif_text is ever implemented this may be handy
                    //OrgKdeKdeconnectDeviceNotificationsNotificationUpdated
                };

                let p4 = c.with_proxy(
                    "org.kde.kdeconnect",
                    "/modules/kdeconnect",
                    Duration::from_millis(5000),
                );

                let _phone_visible_handler = p4.match_signal(
                    move |s: OrgKdeKdeconnectDaemonDeviceVisibilityChanged,
                          _: &Connection,
                          _: &Message| {
                        // TODO: check if s.id matches our device? Is visible same as reachable?
                        let mut reachable = reachable_copy2.lock().unwrap();
                        *reachable = s.is_visible;

                        // Tell block to update now.
                        send7
                            .send(Task {
                                id,
                                update_time: Instant::now(),
                            })
                            .unwrap();

                        true
                    },
                );

                loop {
                    c.process(Duration::from_millis(1000)).unwrap();
                }
            })
            .unwrap();

        Ok(KDEConnect {
            id,
            device_id,
            device_name,
            battery_charge: charge,
            battery_state: charging,
            notif_count,
            // TODO
            //notif_text,
            phone_reachable: reachable,
            bat_good: block_config.bat_good,
            bat_info: block_config.bat_info,
            bat_warning: block_config.bat_warning,
            bat_critical: block_config.bat_critical,
            format: FormatTemplate::from_string(&block_config.format)?,
            format_disconnected: FormatTemplate::from_string(&block_config.format_disconnected)?,
            output: TextWidget::new(id, 0, shared_config.clone()).with_icon("phone")?,
            shared_config,
        })
    }
}

impl Block for KDEConnect {
    fn id(&self) -> usize {
        self.id
    }

    fn update(&mut self) -> Result<Option<Update>> {
        let charge = (*self
            .battery_charge
            .lock()
            .block_error("kdeconnect", "failed to acquire lock for `charge`")?)
            as i32;

        let charging = *self
            .battery_state
            .lock()
            .block_error("kdeconnect", "failed to acquire lock for `battery_state`")?;

        let notif_count = *self
            .notif_count
            .lock()
            .block_error("kdeconnect", "failed to acquire lock for `notif_count`")?;

        // TODO
        //let notif_text = (*self
        //   .notif_text
        //   .lock()
        //   .block_error("kdeconnect", "failed to acquire lock for `notif_text`")?)
        //   .clone();

        let phone_reachable = *self
            .phone_reachable
            .lock()
            .block_error("kdeconnect", "failed to acquire lock for `phone_reachable`")?;

        let name = (*self
            .device_name
            .lock()
            .block_error("kdeconnect", "failed to acquire lock for `name`")?)
        .clone();

        let bat_icon = self
            .shared_config
            .get_icon(if charging {
                // TODO: there are icons for different states of charging
                "bat_charging"
            } else if charge < 0 {
                // better than nothing I guess?
                "bat_full"
            } else {
                battery_level_to_icon(Ok(charge as u64))
            })
            .unwrap_or_default();

        let values = map!(
            "bat_icon" => Value::from_string(bat_icon.trim().to_string()),
            "bat_charge" => Value::from_integer(charge.clamp(0,100) as i64).percents(),
            "bat_state" => Value::from_string(charging.to_string()),
            "notif_icon" => Value::from_string(self.shared_config.get_icon("notification").unwrap_or_default().trim().to_string()),
            "notif_count" => Value::from_integer(notif_count as i64),
            // TODO
            //"notif_text" => notif_text,
            "name" => Value::from_string(name),
            "id" => Value::from_string(self.device_id.to_string()) // Not a String?
        );

        if (
            self.bat_critical,
            self.bat_warning,
            self.bat_info,
            self.bat_good,
        ) == (0, 0, 0, 0)
        {
            self.output.set_state(match notif_count {
                0 => State::Idle,
                _ => State::Info,
            })
        } else if charging {
            self.output.set_state(State::Good);
        } else {
            self.output.set_state(if charge <= self.bat_critical {
                State::Critical
            } else if charge <= self.bat_warning {
                State::Warning
            } else if charge <= self.bat_info {
                State::Info
            } else if charge > self.bat_good {
                State::Good
            } else {
                State::Idle
            });
        }

        if !phone_reachable {
            self.output.set_state(State::Critical);
            self.output.set_icon("phone_disconnected")?;
            self.output
                .set_text(self.format_disconnected.render(&values)?);
        } else {
            self.output.set_icon("phone")?;
            self.output.set_text(self.format.render(&values)?);
        }

        Ok(None)
    }

    // Returns the view of the block, comprised of widgets.
    fn view(&self) -> Vec<&dyn I3BarWidget> {
        vec![&self.output]
    }
}

// Code below generated using the command below and Results changed to explcitly use std::Result
// `dbus-codegen-rust -d org.kde.kdeconnect -p /modules/kdeconnect/devices/mydeviceid/battery`
#[derive(Debug)]
pub struct OrgKdeKdeconnectDeviceBatteryRefreshed {
    pub is_charging: bool,
    pub charge: i32,
}

impl arg::AppendAll for OrgKdeKdeconnectDeviceBatteryRefreshed {
    fn append(&self, i: &mut arg::IterAppend) {
        arg::RefArg::append(&self.is_charging, i);
        arg::RefArg::append(&self.charge, i);
    }
}

impl arg::ReadAll for OrgKdeKdeconnectDeviceBatteryRefreshed {
    fn read(i: &mut arg::Iter) -> std::result::Result<Self, arg::TypeMismatchError> {
        Ok(OrgKdeKdeconnectDeviceBatteryRefreshed {
            is_charging: i.read()?,
            charge: i.read()?,
        })
    }
}

// Code below generated using the command below and Results changed to explcitly use std::Result
// `dbus-codegen-rust -d org.kde.kdeconnect -p /modules/kdeconnect/devices/mydeviceid/notifications`
#[derive(Debug)]
pub struct OrgKdeKdeconnectDeviceNotificationsNotificationPosted {
    pub public_id: String,
}

impl arg::AppendAll for OrgKdeKdeconnectDeviceNotificationsNotificationPosted {
    fn append(&self, i: &mut arg::IterAppend) {
        arg::RefArg::append(&self.public_id, i);
    }
}

impl arg::ReadAll for OrgKdeKdeconnectDeviceNotificationsNotificationPosted {
    fn read(i: &mut arg::Iter) -> std::result::Result<Self, arg::TypeMismatchError> {
        Ok(OrgKdeKdeconnectDeviceNotificationsNotificationPosted {
            public_id: i.read()?,
        })
    }
}

impl dbus::message::SignalArgs for OrgKdeKdeconnectDeviceNotificationsNotificationPosted {
    const NAME: &'static str = "notificationPosted";
    const INTERFACE: &'static str = "org.kde.kdeconnect.device.notifications";
}

#[derive(Debug)]
pub struct OrgKdeKdeconnectDeviceNotificationsNotificationRemoved {
    pub public_id: String,
}

impl arg::AppendAll for OrgKdeKdeconnectDeviceNotificationsNotificationRemoved {
    fn append(&self, i: &mut arg::IterAppend) {
        arg::RefArg::append(&self.public_id, i);
    }
}

impl arg::ReadAll for OrgKdeKdeconnectDeviceNotificationsNotificationRemoved {
    fn read(i: &mut arg::Iter) -> std::result::Result<Self, arg::TypeMismatchError> {
        Ok(OrgKdeKdeconnectDeviceNotificationsNotificationRemoved {
            public_id: i.read()?,
        })
    }
}

impl dbus::message::SignalArgs for OrgKdeKdeconnectDeviceNotificationsNotificationRemoved {
    const NAME: &'static str = "notificationRemoved";
    const INTERFACE: &'static str = "org.kde.kdeconnect.device.notifications";
}

#[derive(Debug)]
pub struct OrgKdeKdeconnectDeviceNotificationsNotificationUpdated {
    pub public_id: String,
}

impl arg::AppendAll for OrgKdeKdeconnectDeviceNotificationsNotificationUpdated {
    fn append(&self, i: &mut arg::IterAppend) {
        arg::RefArg::append(&self.public_id, i);
    }
}

impl arg::ReadAll for OrgKdeKdeconnectDeviceNotificationsNotificationUpdated {
    fn read(i: &mut arg::Iter) -> std::result::Result<Self, arg::TypeMismatchError> {
        Ok(OrgKdeKdeconnectDeviceNotificationsNotificationUpdated {
            public_id: i.read()?,
        })
    }
}

impl dbus::message::SignalArgs for OrgKdeKdeconnectDeviceNotificationsNotificationUpdated {
    const NAME: &'static str = "notificationUpdated";
    const INTERFACE: &'static str = "org.kde.kdeconnect.device.notifications";
}

#[derive(Debug)]
pub struct OrgKdeKdeconnectDeviceNotificationsAllNotificationsRemoved {}

impl arg::AppendAll for OrgKdeKdeconnectDeviceNotificationsAllNotificationsRemoved {
    fn append(&self, _: &mut arg::IterAppend) {}
}

impl arg::ReadAll for OrgKdeKdeconnectDeviceNotificationsAllNotificationsRemoved {
    fn read(_: &mut arg::Iter) -> std::result::Result<Self, arg::TypeMismatchError> {
        Ok(OrgKdeKdeconnectDeviceNotificationsAllNotificationsRemoved {})
    }
}

impl dbus::message::SignalArgs for OrgKdeKdeconnectDeviceNotificationsAllNotificationsRemoved {
    const NAME: &'static str = "allNotificationsRemoved";
    const INTERFACE: &'static str = "org.kde.kdeconnect.device.notifications";
}

impl dbus::message::SignalArgs for OrgKdeKdeconnectDeviceBatteryRefreshed {
    const NAME: &'static str = "refreshed";
    const INTERFACE: &'static str = "org.kde.kdeconnect.device.battery";
}

// Code below generated using the command below and Results changed to explcitly use std::Result
// `dbus-codegen-rust -d org.kde.kdeconnect -p /modules/kdeconnect/devices/mydeviceid`
#[derive(Debug)]
pub struct OrgKdeKdeconnectDeviceNameChanged {
    pub name: String,
}

impl arg::AppendAll for OrgKdeKdeconnectDeviceNameChanged {
    fn append(&self, i: &mut arg::IterAppend) {
        arg::RefArg::append(&self.name, i);
    }
}

impl arg::ReadAll for OrgKdeKdeconnectDeviceNameChanged {
    fn read(i: &mut arg::Iter) -> std::result::Result<Self, arg::TypeMismatchError> {
        Ok(OrgKdeKdeconnectDeviceNameChanged { name: i.read()? })
    }
}

impl dbus::message::SignalArgs for OrgKdeKdeconnectDeviceNameChanged {
    const NAME: &'static str = "nameChanged";
    const INTERFACE: &'static str = "org.kde.kdeconnect.device";
}

#[derive(Debug)]
pub struct OrgKdeKdeconnectDeviceReachableChanged {
    pub reachable: bool,
}

impl arg::AppendAll for OrgKdeKdeconnectDeviceReachableChanged {
    fn append(&self, i: &mut arg::IterAppend) {
        arg::RefArg::append(&self.reachable, i);
    }
}

impl arg::ReadAll for OrgKdeKdeconnectDeviceReachableChanged {
    fn read(i: &mut arg::Iter) -> std::result::Result<Self, arg::TypeMismatchError> {
        Ok(OrgKdeKdeconnectDeviceReachableChanged {
            reachable: i.read()?,
        })
    }
}

impl dbus::message::SignalArgs for OrgKdeKdeconnectDeviceReachableChanged {
    const NAME: &'static str = "reachableChanged";
    const INTERFACE: &'static str = "org.kde.kdeconnect.device";
}

// This code was autogenerated using the command below and Results changed to explcitly use std::Result
// `dbus-codegen-rust -d org.kde.kdeconnect -p /modules/kdeconnect`
#[derive(Debug)]
pub struct OrgKdeKdeconnectDaemonDeviceAdded {
    pub id: String,
}

impl arg::AppendAll for OrgKdeKdeconnectDaemonDeviceAdded {
    fn append(&self, i: &mut arg::IterAppend) {
        arg::RefArg::append(&self.id, i);
    }
}

impl arg::ReadAll for OrgKdeKdeconnectDaemonDeviceAdded {
    fn read(i: &mut arg::Iter) -> std::result::Result<Self, arg::TypeMismatchError> {
        Ok(OrgKdeKdeconnectDaemonDeviceAdded { id: i.read()? })
    }
}

impl dbus::message::SignalArgs for OrgKdeKdeconnectDaemonDeviceAdded {
    const NAME: &'static str = "deviceAdded";
    const INTERFACE: &'static str = "org.kde.kdeconnect.daemon";
}

#[derive(Debug)]
pub struct OrgKdeKdeconnectDaemonDeviceRemoved {
    pub id: String,
}

impl arg::AppendAll for OrgKdeKdeconnectDaemonDeviceRemoved {
    fn append(&self, i: &mut arg::IterAppend) {
        arg::RefArg::append(&self.id, i);
    }
}

impl arg::ReadAll for OrgKdeKdeconnectDaemonDeviceRemoved {
    fn read(i: &mut arg::Iter) -> std::result::Result<Self, arg::TypeMismatchError> {
        Ok(OrgKdeKdeconnectDaemonDeviceRemoved { id: i.read()? })
    }
}

impl dbus::message::SignalArgs for OrgKdeKdeconnectDaemonDeviceRemoved {
    const NAME: &'static str = "deviceRemoved";
    const INTERFACE: &'static str = "org.kde.kdeconnect.daemon";
}

#[derive(Debug)]
pub struct OrgKdeKdeconnectDaemonDeviceVisibilityChanged {
    pub id: String,
    pub is_visible: bool,
}

impl arg::AppendAll for OrgKdeKdeconnectDaemonDeviceVisibilityChanged {
    fn append(&self, i: &mut arg::IterAppend) {
        arg::RefArg::append(&self.id, i);
        arg::RefArg::append(&self.is_visible, i);
    }
}

impl arg::ReadAll for OrgKdeKdeconnectDaemonDeviceVisibilityChanged {
    fn read(i: &mut arg::Iter) -> std::result::Result<Self, arg::TypeMismatchError> {
        Ok(OrgKdeKdeconnectDaemonDeviceVisibilityChanged {
            id: i.read()?,
            is_visible: i.read()?,
        })
    }
}

impl dbus::message::SignalArgs for OrgKdeKdeconnectDaemonDeviceVisibilityChanged {
    const NAME: &'static str = "deviceVisibilityChanged";
    const INTERFACE: &'static str = "org.kde.kdeconnect.daemon";
}

#[derive(Debug)]
pub struct OrgKdeKdeconnectDaemonDeviceListChanged {}

impl arg::AppendAll for OrgKdeKdeconnectDaemonDeviceListChanged {
    fn append(&self, _: &mut arg::IterAppend) {}
}

impl arg::ReadAll for OrgKdeKdeconnectDaemonDeviceListChanged {
    fn read(_: &mut arg::Iter) -> std::result::Result<Self, arg::TypeMismatchError> {
        Ok(OrgKdeKdeconnectDaemonDeviceListChanged {})
    }
}

impl dbus::message::SignalArgs for OrgKdeKdeconnectDaemonDeviceListChanged {
    const NAME: &'static str = "deviceListChanged";
    const INTERFACE: &'static str = "org.kde.kdeconnect.daemon";
}

// these are for kdeconnect versions 20.08.3 and lower
#[derive(Debug)]
pub struct OrgKdeKdeconnectDeviceBatteryStateChanged {
    pub charging: bool,
}

impl arg::AppendAll for OrgKdeKdeconnectDeviceBatteryStateChanged {
    fn append(&self, i: &mut arg::IterAppend) {
        arg::RefArg::append(&self.charging, i);
    }
}

impl arg::ReadAll for OrgKdeKdeconnectDeviceBatteryStateChanged {
    fn read(i: &mut arg::Iter) -> std::result::Result<Self, arg::TypeMismatchError> {
        Ok(OrgKdeKdeconnectDeviceBatteryStateChanged {
            charging: i.read()?,
        })
    }
}

impl dbus::message::SignalArgs for OrgKdeKdeconnectDeviceBatteryStateChanged {
    const NAME: &'static str = "stateChanged";
    const INTERFACE: &'static str = "org.kde.kdeconnect.device.battery";
}

#[derive(Debug)]
pub struct OrgKdeKdeconnectDeviceBatteryChargeChanged {
    pub charge: i32,
}

impl arg::AppendAll for OrgKdeKdeconnectDeviceBatteryChargeChanged {
    fn append(&self, i: &mut arg::IterAppend) {
        arg::RefArg::append(&self.charge, i);
    }
}

impl arg::ReadAll for OrgKdeKdeconnectDeviceBatteryChargeChanged {
    fn read(i: &mut arg::Iter) -> std::result::Result<Self, arg::TypeMismatchError> {
        Ok(OrgKdeKdeconnectDeviceBatteryChargeChanged { charge: i.read()? })
    }
}

impl dbus::message::SignalArgs for OrgKdeKdeconnectDeviceBatteryChargeChanged {
    const NAME: &'static str = "chargeChanged";
    const INTERFACE: &'static str = "org.kde.kdeconnect.device.battery";
}