summaryrefslogtreecommitdiff
path: root/src/deck_convert_gui.adb
blob: 0c352ed4089bd11fdd2f5220e74237051900fe99 (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
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


with

    Ada.Characters.Handling,
    Ada.Directories,
    Ada.Exceptions,
    Ada.Strings.Unbounded,
    Ada.Text_IO,
    Datatypes,
    Deck_IO,
    FLTK.Dialogs,
    FLTK.Widgets.Boxes,
    FLTK.Widgets.Buttons.Enter,
    FLTK.Widgets.Buttons.Light.Check,
    FLTK.Widgets.Inputs.File,
    FLTK.Widgets.Groups.Input_Choices,
    FLTK.Widgets.Groups.Windows.Double,
    FLTK.Widgets.Progress_Bars;

use

    Ada.Text_IO,
    Datatypes;


package body Deck_Convert_GUI is


    package Charhand renames Ada.Characters.Handling;
    package FD renames Ada.Directories;
    package SU renames Ada.Strings.Unbounded;


    function "+"
           (S : in String)
        return SU.Unbounded_String
        renames SU.To_Unbounded_String;

    function "-"
           (US : in SU.Unbounded_String)
        return String
        renames SU.To_String;




    My_Window : FLTK.Widgets.Groups.Windows.Double.Double_Window :=
        FLTK.Widgets.Groups.Windows.Double.Forge.Create
            (500, 280, "Anki to Fresh Memory Deck Converter");


    Deck_Input : FLTK.Widgets.Inputs.File.File_Input :=
        FLTK.Widgets.Inputs.File.Forge.Create (120, 20, 300, 40, "Input filename:");

    Basename_Input : FLTK.Widgets.Inputs.File.File_Input :=
        FLTK.Widgets.Inputs.File.Forge.Create (120, 80, 350, 40, "Output base:");

    Fileload_Button : FLTK.Widgets.Buttons.Button :=
        FLTK.Widgets.Buttons.Forge.Create (430, 25, 30, 30, "+");

    Force_Check : FLTK.Widgets.Buttons.Light.Check.Check_Button :=
        FLTK.Widgets.Buttons.Light.Check.Forge.Create (50, 150, 20, 20, "Overwrite");

    Strip_Check : FLTK.Widgets.Buttons.Light.Check.Check_Button :=
        FLTK.Widgets.Buttons.Light.Check.Forge.Create (50, 180, 20, 20, "Strip Formatting");

    Type_Choice : FLTK.Widgets.Groups.Input_Choices.Input_Choice :=
        FLTK.Widgets.Groups.Input_Choices.Forge.Create (250, 160, 60, 30);

    Start_Button : FLTK.Widgets.Buttons.Enter.Enter_Button :=
        FLTK.Widgets.Buttons.Enter.Forge.Create (370, 160, 80, 30, "Start");

    Status_Box : FLTK.Widgets.Boxes.Box :=
        FLTK.Widgets.Boxes.Forge.Create (0, 220, 500, 40, "STATUS: Awaiting orders, sir!");

    My_Progress : FLTK.Widgets.Progress_Bars.Progress_Bar :=
        FLTK.Widgets.Progress_Bars.Forge.Create (0, 260, 500, 20);


    Status_Black : FLTK.Color := FLTK.No_Color;
    Status_Green : FLTK.Color := 16#00550000#;
    Status_Red   : FLTK.Color := FLTK.Red_Color;
    Bar_Blue     : FLTK.Color := 16#00005500#;




    procedure Activate_All is
    begin
        Deck_Input.Activate;
        Basename_Input.Activate;
        Fileload_Button.Activate;
        Force_Check.Activate;
        Strip_Check.Activate;
        Type_Choice.Activate;
        Start_Button.Activate;
    end Activate_All;


    procedure Deactivate_All is
    begin
        Deck_Input.Deactivate;
        Basename_Input.Deactivate;
        Fileload_Button.Deactivate;
        Force_Check.Deactivate;
        Strip_Check.Deactivate;
        Type_Choice.Deactivate;
        Start_Button.Deactivate;
    end Deactivate_All;




    procedure Fileload_Callback
           (Item : in out FLTK.Widgets.Widget'Class)
    is
        Filename : String := FLTK.Dialogs.File_Chooser ("Select File?", "*", "", True);
    begin
        Deck_Input.Set_Value (Filename);
        Basename_Input.Set_Value (FD.Base_Name (Filename));
    exception
        when Fail : FD.Name_Error =>
            Ada.Text_IO.Put_Line (Standard_Error, "WARNING: Invalid filename selected");
    end Fileload_Callback;


    procedure Start_Callback
           (Item : in out FLTK.Widgets.Widget'Class) is
    begin
        Deactivate_All;
        My_Window.Redraw;
        Worker.Start_Process;
    end Start_Callback;


    procedure Quit_Callback
           (Item : in out FLTK.Widgets.Widget'Class) is
    begin
        abort Worker;
        Hide;
    end Quit_Callback;




    procedure Show is
    begin
        My_Window.Show;
    end Show;


    procedure Hide is
    begin
        My_Window.Hide;
    end Hide;




    procedure Strip_Formatting
           (Notes : in out Note_Vector)
    is
        procedure Strip
               (Text : in out Note;
                Item : in     String)
        is
            Position : Natural;
        begin
            for F of Text.Fields loop
                loop
                    Position := SU.Index
                       (Source  => SU.Unbounded_String (F),
                        Pattern => Item,
                        Going   => Ada.Strings.Forward,
                        Mapping => Charhand.To_Lower'Access);
                    exit when Position = 0;
                    SU.Delete (SU.Unbounded_String (F), Position, Position + Item'Length - 1);
                end loop;
            end loop;
        end Strip;

        Formatting : array (Positive range <>) of SU.Unbounded_String :=
           (+"<b>", +"</b>", +"<strong>", +"</strong>", +"<i>", +"</i>", +"<em>", +"</em>",
            +"<mark>", +"</mark>", +"<small>", +"</small>", +"<del>", +"</del>",
            +"<ins>", +"</ins>", +"<sub>", +"</sub>", +"<sup>", +"</sup>");
    begin
        for N of Notes loop
            for S of Formatting loop
                Strip (N, -S);
            end loop;
        end loop;
    end Strip_Formatting;




    task body Worker is
        use type FLTK.Widgets.Buttons.State;
        use type SU.Unbounded_String;

        Deck_Format : SU.Unbounded_String;
        Input_Name  : SU.Unbounded_String;
        Output_Name : SU.Unbounded_String;

        Overwrite  : Boolean;
        Strip_Form : Boolean;

        Deck   : Deck_IO.Deck_Handle;
        Models : Model_Map;
        Notes  : Note_Vector;
        Media  : Media_Collection;
    begin
        loop
            accept Start_Process;
            My_Progress.Set_Value (0.0);
            My_Progress.Redraw;
            Models.Clear;
            Notes.Clear;
            Media.Map.Clear;

            Deck_Format := +Type_Choice.Get_Input;
            Input_Name  := +Deck_Input.Get_Value;
            Output_Name := +Basename_Input.Get_Value;

            Overwrite  := Force_Check.Get_State = FLTK.Widgets.Buttons.On;
            Strip_Form := Strip_Check.Get_State = FLTK.Widgets.Buttons.On;

            if Deck_Format = "" then
                Put_Line (Standard_Error, "WARNING: No output deck format selected. " &
                    "Proceeding with FMD as default.");
                Deck_Format := +"fmd";
            else
                Deck_Format := +(Charhand.To_Lower (-Deck_Format));
            end if;

            if Deck_Format /= "csv" and Deck_Format /= "fmd" then
                Status_Box.Set_Label ("ERROR: Output deck format must be csv or fmd");
                Status_Box.Set_Label_Color (Status_Red);
                Status_Box.Redraw_Label;
            elsif Input_Name = "" then
                Status_Box.Set_Label ("ERROR: No input deck provided");
                Status_Box.Set_Label_Color (Status_Red);
                Status_Box.Redraw_Label;
            elsif not FD.Exists (-Input_Name) then
                Status_Box.Set_Label ("ERROR: Input deck does not exist");
                Status_Box.Set_Label_Color (Status_Red);
                Status_Box.Redraw_Label;
            elsif Output_Name = "" then
                Status_Box.Set_Label ("ERROR: No output base name provided");
                Status_Box.Set_Label_Color (Status_Red);
                Status_Box.Redraw_Label;
            elsif FD.Exists ((-Output_Name) & "." & (-Deck_Format)) and not Overwrite then
                Status_Box.Set_Label ("ERROR: Output deck file name already exists");
                Status_Box.Set_Label_Color (Status_Red);
                Status_Box.Redraw_Label;
            else
                Status_Box.Set_Label_Color (Status_Black);
                Status_Box.Set_Label ("MESSAGE: Opening input deck database...");
                Status_Box.Redraw_Label;
                Deck_IO.Open_Database (-Input_Name, Deck);
                My_Progress.Set_Value (15.0);
                My_Progress.Redraw;
                Status_Box.Set_Label ("MESSAGE: Querying models...");
                Status_Box.Redraw_Label;
                Deck_IO.Query_Models (Deck, Models);
                My_Progress.Set_Value (30.0);
                My_Progress.Redraw;
                Status_Box.Set_Label ("MESSAGE: Querying notes...");
                Status_Box.Redraw_Label;
                Deck_IO.Query_Notes (Deck, Notes);
                My_Progress.Set_Value (45.0);
                My_Progress.Redraw;
                Status_Box.Set_Label ("MESSAGE: Closing database...");
                Status_Box.Redraw_Label;
                Deck_IO.Close_Database (Deck);
                My_Progress.Set_Value (60.0);
                My_Progress.Redraw;
                if Strip_Form then
                    Status_Box.Set_Label ("MESSAGE: Stripping formatting from notes...");
                    Status_Box.Redraw_Label;
                    Strip_Formatting (Notes);
                    My_Progress.Set_Value (70.0);
                    My_Progress.Redraw;
                end if;
                declare
                    Contain_Dir : String := FD.Containing_Directory (-Output_Name);
                    Simple_Name : String := FD.Simple_Name (-Output_Name);
                begin
                    if Deck_Format = "csv" then
                        Status_Box.Set_Label ("MESSAGE: Writing output to csv...");
                        Status_Box.Redraw_Label;
                        Deck_IO.Write_CSV (Contain_Dir, Simple_Name, Models, Notes, Overwrite);
                        My_Progress.Set_Value (100.0);
                        My_Progress.Redraw;
                        Status_Box.Set_Label ("STATUS: Complete success!");
                        Status_Box.Set_Label_Color (Status_Green);
                        Status_Box.Redraw_Label;
                    elsif Deck_Format = "fmd" then
                        Status_Box.Set_Label ("MESSAGE: Reading media...");
                        Status_Box.Redraw_Label;
                        Deck_IO.Read_Media_Collection (-Input_Name, Media);
                        My_Progress.Set_Value (85.0);
                        My_Progress.Redraw;
                        Status_Box.Set_Label ("MESSAGE: Writing output to fmd...");
                        Status_Box.Redraw_Label;
                        Deck_IO.Write_FMD
                           (Contain_Dir, Simple_Name, Models, Notes, Media, Overwrite);
                        My_Progress.Set_Value (100.0);
                        My_Progress.Redraw;
                        Status_Box.Set_Label ("STATUS: Complete success!");
                        Status_Box.Set_Label_Color (Status_Green);
                        Status_Box.Redraw_Label;
                    else
                        Status_Box.Set_Label ("ERROR: This shouldn't happen!?");
                        Status_Box.Set_Label_Color (Status_Red);
                        Status_Box.Redraw_Label;
                    end if;
                end;
            end if;

            Activate_All;
            My_Window.Redraw;
        end loop;
    end Worker;


begin


    My_Window.Add (Deck_Input);
    My_Window.Add (Basename_Input);

    Fileload_Button.Set_Callback (Fileload_Callback'Access);
    My_Window.Add (Fileload_Button);

    My_Window.Add (Force_Check);
    My_Window.Add (Strip_Check);

    Type_Choice.Menu_Button.Add ("fmd");
    Type_Choice.Menu_Button.Add ("csv");
    My_Window.Add (Type_Choice);

    Status_Box.Set_Box (FLTK.No_Box);
    Status_Box.Set_Label_Color (Status_Green);
    My_Window.Add (Status_Box);

    Start_Button.Set_Callback (Start_Callback'Access);
    My_Window.Add (Start_Button);

    My_Progress.Set_Selection_Color (Bar_Blue);
    My_Progress.Set_Minimum (0.0);
    My_Progress.Set_Maximum (100.0);
    My_Window.Add (My_Progress);

    My_Window.Set_Callback (Quit_Callback'Access);


end Deck_Convert_GUI;