aboutsummaryrefslogtreecommitdiff
path: root/ShiftOS.WinForms/Applications/FileDialog.cs
blob: 333a9b72bde105a854324ae5087b406e0d6eeac0 (plain) (blame)
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
using ShiftOS.Objects.ShiftFS;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static ShiftOS.Objects.ShiftFS.Utils;
using ShiftOS.Engine;
using ShiftOS.WinForms.Tools;

namespace ShiftOS.WinForms.Applications
{
    /// <summary>
    /// 
    /// </summary>
    public partial class FileDialog : UserControl, IShiftOSWindow
    {
        public FileDialog(string[] filetypes, FileOpenerStyle style, Action<string> _callback)
        {
            callback = _callback;
            InitializeComponent();
            foreach(var itm in filetypes)
            {
                cbfiletypes.Items.Add(itm);
            }
            cbfiletypes.SelectedIndex = 0;
            cbfiletypes.SelectedIndexChanged += (o, a) => { ResetList(); };
            this.lvitems.SelectedIndexChanged += (o, a) =>
            {
                try
                {
                    var itm = lvitems.SelectedItems[0];
                    if (FileExists(currentdir + "/" + itm.Text))
                    {
                        txtfilename.Text = itm.Text;
                    }
                }
                catch { }

            };
            btnok.Click += (o, a) =>
            {
                string fname = "";
                fname = (!string.IsNullOrWhiteSpace(txtfilename.Text)) ? txtfilename.Text : "";
                fname = (!fname.EndsWith(cbfiletypes.SelectedItem.ToString())) ? fname + cbfiletypes.SelectedItem.ToString() : fname;
                fname = (fname == cbfiletypes.SelectedItem.ToString()) ? "" : fname;

                switch (style)
                {

                    case FileOpenerStyle.Open:
                        

                        if(FileExists(currentdir + "/" + fname))
                        {
                            callback?.Invoke(currentdir + "/" + fname);
                            this.Close();
                        }
                        else
                        {
                            Infobox.Show("{FILE_NOT_FOUND}", "{FILE_NOT_FOUND_EXP}");
                        }
                        break;
                    case FileOpenerStyle.Save:
                        if (!string.IsNullOrWhiteSpace(fname))
                        {
                            callback?.Invoke(currentdir + "/" + fname);
                            this.Close();
                        }
                        else
                        {
                            Infobox.Show("{ENTER_FILENAME}", "{ENTER_FILENAME_EXP}");
                        }
                        break;
                }
            };
            btnok.Text = style.ToString();
            this.Text = style.ToString() + " File";
            this.lvitems.DoubleClick += new EventHandler(this.lvitems_DoubleClick);
            this.Load += (o, a) =>
            {
                ChangeDirectory(Paths.GetPath("root"));
            };
        }

        private void lvitems_DoubleClick(object sender, EventArgs e)
        {
            if (lvitems.SelectedItems.Count <= 0)
                return;

            var item = lvitems.SelectedItems[0];
            var path = item.Tag as string;
            if (currentdir == "__system")
            {
                ChangeDirectory(path);
            }
            else if (DirectoryExists(currentdir + "/" + path))
            {
                ChangeDirectory(currentdir + "/" + path);
            }
            else if (FileExists(currentdir + "/" + path))
            {
                callback?.Invoke(currentdir + "/" + txtfilename.Text);
                this.Close();
            }
            else if (path == "__..")
            {
                ChangeToParent();
            }
        }

        Action<string> callback;

        string currentdrive = "0:";

        public void ChangeToParent()
        {
            if (currentdir == currentdrive)
            {
                ChangeDirectory("__system");
            }

            ChangeDirectory(GetParent(currentdir));
        }

        public string GetParent(string path)
        {
            string[] pathlist = path.Split(new[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
            if (pathlist.Length > 1)
            {
                if (path.EndsWith("/"))
                {
                    path = path.Remove(path.Length - 1, 1);
                }
                path = path.Remove(path.LastIndexOf('/'), path.Length - path.LastIndexOf('/'));
                return path;
            }
            else
            {
                return "__system";
            }
        }

        private string currentdir = "";

        public void ChangeDirectory(string path)
        {
            currentdir = path;
            lbcurrentfolder.Text = currentdir;
            ResetList();
        }

        public void ResetList()
        {
            if (lvitems.LargeImageList == null)
            {
                lvitems.LargeImageList = new ImageList();
                lvitems.LargeImageList.TransparentColor = SkinEngine.LoadedSkin.ControlColor;
                lvitems.LargeImageList.ImageSize = new Size(42, 42);
                FileSkimmer.GetAllTypes(lvitems.LargeImageList);
            }


            lvitems.Items.Clear();

            if (currentdir == "__system")
            {
                //List all drives
                foreach (var dir in Mounts)
                {
                    var item = FileSkimmer.ConstructItemAsMount(dir);
                    item.ImageKey = "Mount";
                    lvitems.Items.Add(item);
                }
            }
            else if (DirectoryExists(currentdir))
            {
                var up = new ListViewItem();
                up.ImageKey = "UpOne";
                up.Text = "Up one";
                up.Tag = "__..";
                lvitems.Items.Add(up);


                foreach (var dir in GetDirectories(currentdir))
                {
                    var item = FileSkimmer.ConstructItem(GetDirectoryInfo(dir));
                    item.ImageKey = "Directory";
                    lvitems.Items.Add(item);
                }

                foreach (var dir in GetFiles(currentdir))
                {
                    if (dir.EndsWith(cbfiletypes.SelectedItem as string))
                    {
                        var item = FileSkimmer.ConstructItem(GetFileInfo(dir));
                        item.ImageKey = FileSkimmerBackend.GetFileType(dir).ToString();
                        lvitems.Items.Add(item);
                    }
                }

            }
        }

        [Obsolete("Use the relevant static method within File Skimmer instead.")]
        public static ListViewItem ConstructItemAsMount(Directory dir)
        {
            var item = new ListViewItem();
            item.Text = dir.Name + "(" + Mounts.IndexOf(dir).ToString() + ":/)";
            item.Tag = Mounts.IndexOf(dir).ToString() + ":";
            return item;
        }


        [Obsolete("Use the relevant static method within File Skimmer instead.")]
        public static ListViewItem ConstructItem(Directory dir)
        {
            var item = new ListViewItem();
            item.Text = dir.Name;
            item.Tag = item.Text;
            return item;
        }

        [Obsolete("Use the relevant static method within File Skimmer instead.")]
        public static ListViewItem ConstructItem(File dir)
        {
            var item = new ListViewItem();
            item.Text = dir.Name;
            item.Tag = item.Text;
            return item;
        }

        public void OnLoad()
        {
        }

        public void OnSkinLoad()
        {
        }

        public bool OnUnload()
        {
            return true;
        }        

        public void OnUpgrade()
        {
        }
    }

    
}