Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FrmInstallPEProfiles.cs « Forms « UVtools.GUI - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9b2ce81f330efbb5d943cee237c394b49303493c (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
/*
 *                     GNU AFFERO GENERAL PUBLIC LICENSE
 *                       Version 3, 19 November 2007
 *  Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
 *  Everyone is permitted to copy and distribute verbatim copies
 *  of this license document, but changing it is not allowed.
 */
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace UVtools.GUI.Forms
{
    public partial class FrmInstallPEProfiles : Form
    {
        private PEProfileFolder[] Profiles { get; }
        public FrmInstallPEProfiles()
        {
            InitializeComponent();
            
            Profiles = new[]
            {
                new PEProfileFolder(PEProfileFolder.FolderType.Print, lvPrintProfiles, lbPrintProfilesCount),
                new PEProfileFolder(PEProfileFolder.FolderType.Printer, lvPrinterProfiles, lbPrinterProfilesCount)
            };

            Init();
        }

        private void Init()
        {
            foreach (var profile in Profiles)
            {
                DirectoryInfo di = new DirectoryInfo(profile.SourcePath);
                var files = di.GetFiles("*.ini");

                byte installedCount = 0;
                byte updateCount = 0;

                profile.ListView.BeginUpdate();
                profile.ListView.Items.Clear();
                foreach (var fileInfo in files)
                {
                    ListViewItem item = new ListViewItem
                    {
                        Text = Path.GetFileNameWithoutExtension(fileInfo.Name),
                        Tag = fileInfo
                    };

                    var targetFile = $"{profile.TargetPath}{Path.DirectorySeparatorChar}{fileInfo.Name}";
                    FileInfo targetFileInfo = new FileInfo(targetFile);
                    if (targetFileInfo.Exists)
                    {
                        installedCount++;
                        if (targetFileInfo.Length != fileInfo.Length || targetFileInfo.LastWriteTime != fileInfo.LastWriteTime)
                        {
                            item.ForeColor = Color.Red;
                            item.Checked = true;
                            updateCount++;
                        }
                        else
                        {
                            item.ForeColor = Color.Green;
                        }
                    }
                    else if (ReferenceEquals(profile.ListView, lvPrintProfiles))
                    {
                        item.Checked = true;
                    }

                    profile.ListView.Items.Add(item);
                }
                profile.ListView.EndUpdate();
                profile.LabelCount.Text = $"{updateCount} Update(s) | {installedCount} Installed | {profile.ListView.Items.Count} Profiles";
            }
        }

        #region Events
        private void EventClick(object sender, EventArgs e)
        {

            if (ReferenceEquals(sender, btnRefreshProfiles))
            {
                Init();
                return;
            }

            if (ReferenceEquals(sender, btnPrintProfilesSelectAll) ||
                ReferenceEquals(sender, btnPrintProfilesUnselectAll) ||
                ReferenceEquals(sender, btnPrinterProfilesSelectAll) ||
                ReferenceEquals(sender, btnPrinterProfilesUnselectAll))
            {
                bool isChecked = ReferenceEquals(sender, btnPrintProfilesSelectAll) ||
                              ReferenceEquals(sender, btnPrinterProfilesSelectAll);

                ListView lv = ReferenceEquals(sender, btnPrintProfilesSelectAll) || ReferenceEquals(sender,
                    btnPrintProfilesUnselectAll) ? lvPrintProfiles : lvPrinterProfiles;

                lv.BeginUpdate();
                foreach (ListViewItem item in lv.Items)
                {
                    item.Checked = isChecked;
                }
                lv.EndUpdate();

                return;
            }


            if (ReferenceEquals(sender, btnInstall))
            {

                var result = MessageBox.Show(
                    "This action will install and override the following profiles into PrusaSlicer:\n" +
                    "---------- PRINT PROFILES ----------\n" +
                    Profiles[0].SelectedFiles +
                    "--------- PRINTER PROFILES ---------\n" +
                    Profiles[1].SelectedFiles +
                    "---------------\n" +
                    "Click 'Yes' to continue\n" +
                    "Click 'No' to cancel this operation",
                    "Install printers into PrusaSlicer", MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question);


                if (result != DialogResult.Yes)
                {
                    return;
                }

                ushort count = 0;

                try
                {
                    foreach (var profile in Profiles)
                    {
                        foreach (ListViewItem item in profile.ListView.Items)
                        {
                            if (!item.Checked) continue;
                            var fi = item.Tag as FileInfo;
                            fi.CopyTo($"{profile.TargetPath}{Path.DirectorySeparatorChar}{fi.Name}", true);
                            count++;
                        }
                    }
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message, "Unable to install the profiles", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    DialogResult = DialogResult.Abort;

                    return;
                }
                

                DialogResult = DialogResult.OK;

                MessageBox.Show(
                    $"Profiles ({count}) were installed.\nRestart PrusaSlicer and check if profiles are present.",
                    "Operation Completed", MessageBoxButtons.OK, MessageBoxIcon.Information);

                return;
            }
        }
        #endregion
    }
}