-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListMailboxWindow.axaml.cs
153 lines (130 loc) · 5.17 KB
/
ListMailboxWindow.axaml.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using PoopmailGui.Api;
using TextCopy;
namespace PoopmailGui
{
public class ListMailboxWindow : Window
{
private readonly string _accessToken;
private readonly ApiClient _apiClient;
private readonly string _refreshToken;
public ListMailboxWindow()
{
InitializeComponent();
#if DEBUG
this.AttachDevTools();
#endif
}
public ListMailboxWindow(ApiClient apiClient, string refreshToken, string accessToken)
{
_apiClient = apiClient;
_refreshToken = refreshToken;
_accessToken = accessToken;
InitializeComponent();
#if DEBUG
this.AttachDevTools();
#endif
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
var listBox = this.FindControl<ListBox>("ListMailboxes");
listBox.SelectionMode = SelectionMode.AlwaysSelected | SelectionMode.Single | SelectionMode.Toggle;
var contextMenu = new ContextMenu {Width = 150, Height = 100};
var menuItems = new List<Control>();
menuItems.Add(MakeItem("Open", (sender, args) => { ButtonOpen_OnClick(null, null); }));
menuItems.Add(MakeItem("Delete", (sender, args) => { HandleDelete(); }));
menuItems.Add(MakeItem("Copy address", (sender, args) =>
{
var listBox = this.FindControl<ListBox>("ListMailboxes");
var selectedItems = listBox.SelectedItems;
if (selectedItems.Count != 1) return;
var item = (ListBoxItem) selectedItems[0];
var content = item.Content;
if (!(content is TextBlock)) return;
string address = ((TextBlock) content).Text;
try
{
ClipboardService.SetText(address);
}
catch (Exception e)
{
new ErrorDialog(600, 200, "Failed to set clipboard (Maybe install xsel?):\n\n" + e.Message).Show();
}
}));
contextMenu.Items = menuItems;
listBox.ContextMenu = contextMenu;
Refresh();
}
private void HandleDelete()
{
var listBox = this.FindControl<ListBox>("ListMailboxes");
var selectedItems = listBox.SelectedItems;
if (selectedItems.Count != 1) return;
var item = (ListBoxItem) selectedItems[0];
var content = item.Content;
if (!(content is TextBlock)) return;
string address = ((TextBlock) content).Text;
if(address.Contains(" ") || !address.Contains("@"))
return;
var result = _apiClient.DeleteMailbox(_refreshToken, _accessToken, address);
if ((int) result.Obj >= 200 && (int) result.Obj < 300)
((TextBlock) content).Text = "Deleted";
else
((TextBlock) content).Text = "Failed to delete: " + result.Obj + ": " + result.Response.Response;
}
public void Refresh()
{
var mailboxes = _apiClient.GetMailboxes(_refreshToken, _accessToken);
if (mailboxes.Obj == null)
return;
if (mailboxes.Obj.Data == null)
return;
var list = mailboxes.Obj.Data
.Select(mailbox => MakeText(mailbox.Address, (o, o1) =>
ButtonOpen_OnClick(null, null))).ToList();
var listBox = this.FindControl<ListBox>("ListMailboxes");
listBox.Items = list;
}
private void Button_OnClick(object? sender, RoutedEventArgs e)
{
var window = new CreateMailboxWindow(this, _apiClient, _refreshToken, _accessToken);
window.Show();
}
private void ButtonOpen_OnClick(object? sender, RoutedEventArgs e)
{
var listBox = this.FindControl<ListBox>("ListMailboxes");
var selectedItems = listBox.SelectedItems;
if (selectedItems.Count != 1) return;
var item = (ListBoxItem) selectedItems[0];
var content = item.Content;
if (!(content is TextBlock)) return;
string txt = ((TextBlock) content).Text;
if(txt.Contains(" ") || !txt.Contains("@"))
return;
var window = new ViewMailboxWindow(txt, _apiClient, _refreshToken, _accessToken);
window.Show();
}
private ListBoxItem MakeText(string str, Action<object, object> action)
{
var item = new ListBoxItem();
item.AddHandler(DoubleTappedEvent, action);
var box = new TextBlock();
box.Text = str;
item.Content = box;
return item;
}
private Control MakeItem(string hellooo, Action<object, object> action)
{
var txt = new TextBlock {Text = hellooo};
txt.AddHandler(TappedEvent, action);
return txt;
}
}
}