aboutsummaryrefslogtreecommitdiff
path: root/ShiftOS.WinForms/WidgetManager.cs
blob: 15e2076d36385f5f1c4bee1d0e829ebdd33773b7 (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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using ShiftOS.Engine;
using ShiftOS.Objects.ShiftFS;

namespace ShiftOS.WinForms
{
    public static class WidgetManager
    {
        public static Dictionary<DesktopWidgetAttribute, Type> GetAllWidgetTypes()
        {
            Dictionary<DesktopWidgetAttribute, Type> types = new Dictionary<WinForms.DesktopWidgetAttribute, Type>();
            foreach(var exe in System.IO.Directory.GetFiles(Environment.CurrentDirectory))
            {
                if(exe.EndsWith(".exe") || exe.EndsWith(".dll"))
                {
                    try
                    {
                        var asm = Assembly.LoadFile(exe);
                        foreach(var type in asm.GetTypes())
                        {
                            if (type.GetInterfaces().Contains(typeof(IDesktopWidget)))
                            {
                                if (Shiftorium.UpgradeAttributesUnlocked(type))
                                {
                                    foreach (var attrib in type.GetCustomAttributes(false))
                                    {
                                        if (attrib is DesktopWidgetAttribute)
                                        {
                                            var dw = attrib as DesktopWidgetAttribute;
                                            types.Add(dw, type);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch { }
                }
            }
            return types;
        }

        internal static void SaveLocation(Type type, Point location)
        {
            var dict = new Dictionary<string, Point>();
            var attrib = type.GetCustomAttributes(false).FirstOrDefault(x => x is DesktopWidgetAttribute) as DesktopWidgetAttribute;
            try
            {
                dict = JsonConvert.DeserializeObject<Dictionary<string, Point>>(Utils.ReadAllText(Paths.GetPath("widgets.dat")));

                dict[attrib.ToString()] = location;
            }
            catch
            {
                dict.Add(attrib.ToString(), location);
            }
            finally
            {
                Utils.WriteAllText(Paths.GetPath("widgets.dat"), JsonConvert.SerializeObject(dict));
            }

        }

        internal static Point LoadLocation(Type type)
        {
            var dict = new Dictionary<string, Point>();
            var attrib = type.GetCustomAttributes(false).FirstOrDefault(x => x is DesktopWidgetAttribute) as DesktopWidgetAttribute;
            try
            {
                dict = JsonConvert.DeserializeObject<Dictionary<string, Point>>(Utils.ReadAllText(Paths.GetPath("widgets.dat")));

                return dict[attrib.ToString()];

            }
            catch
            {
                return new Point(-1, -1);
            }
            finally
            {
            }

        }
    }
}