qleany 1.7.3

Architecture generator for Rust and C++/Qt applications.
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
//! Home Tab module
//!
//! This module contains the logic specific to the Home tab,
//! including manifest-related callback handlers (new, open, save, close, open_qleany).

use std::sync::Arc;

use common::event::{HandlingManifestEvent, Origin};
use handling_manifest::LoadDto;
use slint::ComponentHandle;

use crate::app_context::AppContext;
use crate::commands::{handling_manifest_commands, workspace_commands};
use crate::event_hub_client::EventHubClient;
use crate::{App, AppState, ManifestCommands, NewManifestWizardState};
use handling_manifest::{CreateLanguage, ManifestTemplate};
use slint::Timer;

fn subscribe_loaded_event(
    event_hub_client: &EventHubClient,
    app: &App,
    app_context: &Arc<AppContext>,
) {
    event_hub_client.subscribe(Origin::HandlingManifest(HandlingManifestEvent::Load), {
        let ctx = Arc::clone(app_context);
        let app_weak = app.as_weak();
        move |event| {
            log::info!("Manifest loaded event received: {:?}", event);
            let _ctx = Arc::clone(&ctx);
            let app_weak = app_weak.clone();

            let _ = slint::invoke_from_event_loop(move || {
                if let Some(app) = app_weak.upgrade() {
                    // clear any previous error
                    app.global::<AppState>()
                        .set_error_message(slint::SharedString::from(""));
                    // set loading
                    app.global::<AppState>().set_is_loading(true);
                    // set workspace_id
                    app.global::<AppState>()
                        .set_workspace_id(event.ids[0] as i32);

                    app.global::<AppState>().set_manifest_is_saved(true);
                    app.global::<AppState>().set_is_loading(false);
                    app.global::<AppState>().set_manifest_is_open(true);
                    if let Some(data) = event.data {
                        app.global::<AppState>()
                            .set_manifest_path(slint::SharedString::from(data));
                    }
                    log::info!("Manifest UI state updated after load");
                }
            });
        }
    });
}

fn subscribe_closed_event(
    event_hub_client: &EventHubClient,
    app: &App,
    app_context: &Arc<AppContext>,
) {
    event_hub_client.subscribe(Origin::HandlingManifest(HandlingManifestEvent::Close), {
        let ctx = Arc::clone(app_context);
        let app_weak = app.as_weak();
        move |event| {
            log::info!("Manifest closed event received: {:?}", event);
            let _ctx = Arc::clone(&ctx);
            let app_weak = app_weak.clone();

            // Use invoke_from_event_loop to safely update UI from background thread
            let _ = slint::invoke_from_event_loop(move || {
                if let Some(app) = app_weak.upgrade() {
                    // clear any previous error
                    app.global::<AppState>()
                        .set_error_message(slint::SharedString::from(""));
                    // set workspace_id
                    app.global::<AppState>().set_workspace_id(-1);

                    app.global::<AppState>().set_manifest_is_saved(true);
                    app.global::<AppState>().set_is_loading(false);
                    app.global::<AppState>().set_manifest_is_open(false);
                    app.global::<AppState>()
                        .set_manifest_path(slint::SharedString::from(""));
                }
            });
        }
    });
}

fn subscribe_new_manifest_event(
    event_hub_client: &EventHubClient,
    app: &App,
    app_context: &Arc<AppContext>,
) {
    event_hub_client.subscribe(Origin::HandlingManifest(HandlingManifestEvent::Create), {
        let ctx = Arc::clone(app_context);
        let app_weak = app.as_weak();
        move |event| {
            log::info!("New manifest created event received");
            let _ctx = Arc::clone(&ctx);
            let app_weak = app_weak.clone();

            let _ = slint::invoke_from_event_loop(move || {
                if let Some(app) = app_weak.upgrade() {
                    // clear any previous error
                    app.global::<AppState>()
                        .set_error_message(slint::SharedString::from(""));
                    // set workspace_id
                    app.global::<AppState>()
                        .set_workspace_id(event.ids[0] as i32);

                    app.global::<AppState>().set_manifest_is_saved(true);
                    app.global::<AppState>().set_is_loading(false);
                    app.global::<AppState>().set_manifest_is_open(true);
                    app.global::<AppState>()
                        .set_manifest_path(slint::SharedString::from(""));
                }
            });
        }
    });
}

/// Wire up the on_new_manifest callback — opens the wizard
pub fn setup_new_manifest_callback(app: &App, app_context: &Arc<AppContext>) {
    app.global::<ManifestCommands>().on_new_manifest({
        let ctx = Arc::clone(app_context);
        let app_weak = app.as_weak();
        move || {
            log::info!("New Manifest clicked — opening wizard");

            if let Some(app) = app_weak.upgrade() {
                if app.global::<AppState>().get_manifest_is_open() {
                    log::info!("A manifest is already open, closing it first");
                    match handling_manifest_commands::close(&ctx) {
                        Ok(()) => {
                            log::info!("Manifest closed successfully");
                        }
                        Err(e) => {
                            log::error!("Failed to close manifest: {}", e);
                            return;
                        }
                    }
                }

                // Reset wizard state and show it
                let wiz = app.global::<NewManifestWizardState>();
                wiz.set_step(0);
                wiz.set_language_index(0);
                wiz.set_application_name(slint::SharedString::from(""));
                wiz.set_organization_name(slint::SharedString::from(""));
                wiz.set_template_index(0);
                wiz.set_opt_rust_cli(false);
                wiz.set_opt_rust_slint(false);
                wiz.set_opt_cpp_qt_qtwidgets(false);
                wiz.set_opt_cpp_qt_qtquick(true);
                wiz.set_name_error(slint::SharedString::from(""));
                wiz.set_org_error(slint::SharedString::from(""));
                wiz.set_visible(true);
            }
        }
    });
}

/// Wire up the create_wizard_confirmed callback — save dialog + create + load
pub fn setup_create_wizard_confirmed_callback(app: &App, app_context: &Arc<AppContext>) {
    app.global::<ManifestCommands>()
        .on_create_wizard_confirmed({
            let ctx = Arc::clone(app_context);
            let app_weak = app.as_weak();
            move || {
                log::info!("Wizard confirmed — proceeding to create manifest");

                let Some(app) = app_weak.upgrade() else {
                    return;
                };

                // Read wizard state
                let wiz = app.global::<NewManifestWizardState>();
                let language = if wiz.get_language_index() == 0 {
                    CreateLanguage::CppQt
                } else {
                    CreateLanguage::Rust
                };
                let application_name = wiz.get_application_name().to_string();
                let organization_name = wiz.get_organization_name().to_string();
                let manifest_template = match wiz.get_template_index() {
                    1 => ManifestTemplate::Minimal,
                    2 => ManifestTemplate::DocumentEditor,
                    3 => ManifestTemplate::DataManagement,
                    _ => ManifestTemplate::Blank,
                };

                let mut options = Vec::new();
                if wiz.get_opt_rust_cli() {
                    options.push("rust_cli".to_string());
                }
                if wiz.get_opt_rust_slint() {
                    options.push("rust_slint".to_string());
                }
                if wiz.get_opt_cpp_qt_qtwidgets() {
                    options.push("cpp_qt_qtwidgets".to_string());
                }
                if wiz.get_opt_cpp_qt_qtquick() {
                    options.push("cpp_qt_qtquick".to_string());
                }

                // Step 1: Save file dialog
                let default_path = dirs::home_dir().unwrap_or_default();
                let file_dialog = rfd::FileDialog::new()
                    .add_filter("YAML files", &["yaml", "yml"])
                    .set_directory(&default_path)
                    .set_file_name("qleany.yaml");

                let path = match file_dialog.save_file() {
                    Some(path) => path,
                    None => {
                        log::info!("Save file dialog cancelled");
                        return;
                    }
                };

                let manifest_path = path.to_string_lossy().to_string();
                log::info!("Selected save path: {}", manifest_path);

                app.global::<AppState>().set_is_loading(true);

                // Step 2: Create manifest via use case
                let create_dto = handling_manifest::CreateDto {
                    manifest_path: manifest_path.clone(),
                    language,
                    application_name,
                    organization_name,
                    manifest_template,
                    options,
                };
                match handling_manifest_commands::create(&ctx, &create_dto) {
                    Ok(_result) => {
                        log::info!("New manifest created successfully at {}", manifest_path);
                    }
                    Err(e) => {
                        log::error!("Failed to create new manifest: {}", e);
                        app.global::<AppState>().set_is_loading(false);
                        app.global::<AppState>()
                            .set_error_message(slint::SharedString::from(e));
                        return;
                    }
                }

                // Step 3: Open the newly created manifest
                let load_dto = LoadDto {
                    manifest_path: manifest_path.clone(),
                };
                match handling_manifest_commands::load(&ctx, &load_dto) {
                    Ok(result) => {
                        log::info!("Manifest loaded successfully: {:?}", result);
                        if let Some(app) = app_weak.upgrade() {
                            app.global::<AppState>()
                                .set_error_message(slint::SharedString::from(""));
                            app.global::<AppState>().set_manifest_is_saved(true);
                            app.global::<AppState>()
                                .set_manifest_path(slint::SharedString::from(manifest_path));

                            app.global::<AppState>().set_success_message(
                                slint::SharedString::from(
                                    "Manifest created and loaded successfully",
                                ),
                            );
                            app.global::<AppState>().set_success_message_visible(true);

                            let app_weak_timer = app.as_weak();
                            Timer::single_shot(std::time::Duration::from_secs(3), move || {
                                if let Some(app) = app_weak_timer.upgrade() {
                                    app.global::<AppState>().set_success_message_visible(false);
                                }
                            });
                        }
                    }
                    Err(e) => {
                        log::error!("Failed to load created manifest: {}", e);
                        app.global::<AppState>().set_is_loading(false);
                        if let Some(app) = app_weak.upgrade() {
                            app.global::<AppState>()
                                .set_error_message(slint::SharedString::from(e));
                        }
                    }
                }
            }
        });
}

/// Wire up the on_open_manifest callback
pub fn setup_open_manifest_callback(app: &App, app_context: &Arc<AppContext>) {
    app.global::<ManifestCommands>().on_open_manifest({
        let ctx = Arc::clone(app_context);
        let app_weak = app.as_weak();
        move || {
            log::info!("Open Manifest clicked");

            // Get the user's home directory as the default path
            let default_path = dirs::home_dir().unwrap_or_default();

            // Open file dialog using rfd
            let file_dialog = rfd::FileDialog::new()
                .add_filter("YAML files", &["yaml", "yml"])
                .set_directory(&default_path)
                .set_file_name("qleany.yaml");

            if let Some(app) = app_weak.upgrade() {
                if let Some(path) = file_dialog.pick_file() {
                    let manifest_path = path.to_string_lossy().to_string();
                    log::info!("Selected manifest file: {}", manifest_path);

                    if app.global::<AppState>().get_manifest_is_open() {
                        log::info!("A manifest is already open, closing it first");

                        // Close any currently open manifest first
                        match handling_manifest_commands::close(&ctx) {
                            Ok(()) => {
                                log::info!("Manifest closed successfully");
                            }
                            Err(e) => {
                                log::error!("Failed to close manifest: {}", e);
                                return;
                            }
                        }
                    }
                    let load_dto = LoadDto { manifest_path };
                    match handling_manifest_commands::load(&ctx, &load_dto) {
                        Ok(result) => {
                            log::info!("Manifest loaded successfully: {:?}", result);
                            if let Some(app) = app_weak.upgrade() {
                                app.global::<AppState>().set_manifest_path(
                                    slint::SharedString::from(load_dto.manifest_path),
                                );
                            }
                        }

                        Err(e) => {
                            log::error!("Failed to load manifest: {}", e);
                            if let Some(app) = app_weak.upgrade() {
                                app.global::<AppState>().set_is_loading(false);
                                app.global::<AppState>()
                                    .set_error_message(slint::SharedString::from(e));
                            }
                        }
                    }
                }
            } else {
                log::info!("File dialog cancelled");
            }
        }
    });
}

/// Wire up the on_save_manifest callback
pub fn setup_save_manifest_as_callback(app: &App, app_context: &Arc<AppContext>) {
    app.global::<ManifestCommands>().on_save_manifest_as({
        let ctx = Arc::clone(app_context);
        let app_weak = app.as_weak();
        move || {
            log::info!("Save Manifest As clicked");

            // Get the user's home directory as the default path
            let default_path = dirs::home_dir().unwrap_or_default();

            // Open save file dialog using rfd
            let file_dialog = rfd::FileDialog::new()
                .add_filter("YAML files", &["yaml", "yml"])
                .set_directory(&default_path)
                .set_file_name("qleany.yaml");

            if let Some(path) = file_dialog.save_file() {
                let manifest_path = path.to_string_lossy().to_string();
                log::info!("Selected save path: {}", manifest_path);

                let save_dto = handling_manifest::SaveDto { manifest_path };
                match handling_manifest_commands::save(&ctx, &save_dto) {
                    Ok(()) => {
                        log::info!("Manifest saved successfully");
                        if let Some(app) = app_weak.upgrade() {
                            app.global::<AppState>()
                                .set_error_message(slint::SharedString::from(""));
                            app.global::<AppState>().set_manifest_is_saved(true);
                            app.global::<AppState>()
                                .set_manifest_path(slint::SharedString::from(
                                    save_dto.manifest_path,
                                ));

                            // Show success message
                            app.global::<AppState>().set_success_message(
                                slint::SharedString::from("Manifest saved successfully"),
                            );
                            app.global::<AppState>().set_success_message_visible(true);

                            // Hide after 3 seconds
                            let app_weak_timer = app.as_weak();
                            Timer::single_shot(std::time::Duration::from_secs(3), move || {
                                if let Some(app) = app_weak_timer.upgrade() {
                                    app.global::<AppState>().set_success_message_visible(false);
                                }
                            });
                        }
                    }
                    Err(e) => {
                        log::error!("Failed to save manifest: {}", e);
                        if let Some(app) = app_weak.upgrade() {
                            app.global::<AppState>()
                                .set_error_message(slint::SharedString::from(e));
                        }
                    }
                }
            } else {
                log::info!("Save file dialog cancelled");
            }
        }
    });
}

/// Wire up the on_save_manifest callback
pub fn setup_save_manifest_callback(app: &App, app_context: &Arc<AppContext>) {
    app.global::<ManifestCommands>().on_save_manifest({
        let ctx = Arc::clone(app_context);
        let app_weak = app.as_weak();
        move || {
            log::info!("Save Manifest clicked");

            let workspace_id = app_weak
                .upgrade()
                .map_or(0, |app| app.global::<AppState>().get_workspace_id() as u64);
            if workspace_id < 1 {
                log::error!("No manifest is currently loaded, cannot save");
                return;
            }
            let workspace =
                workspace_commands::get_workspace(&ctx, &workspace_id as &common::types::EntityId)
                    .ok()
                    .flatten();
            if workspace.is_none() {
                log::error!("Failed to get workspace entity for id {}", workspace_id);
                return;
            }
            let manifest_absolute_path = workspace.unwrap().manifest_absolute_path;
            log::info!("Saving manifest to path: {}", manifest_absolute_path);

            // add "qleany.yaml" if manifest_absolute_path is a directory
            let manifest_absolute_path = {
                let path = std::path::Path::new(&manifest_absolute_path);
                if path.is_dir() {
                    path.join("qleany.yaml").to_string_lossy().to_string()
                } else {
                    manifest_absolute_path
                }
            };

            let save_dto = handling_manifest::SaveDto {
                manifest_path: manifest_absolute_path,
            };
            match handling_manifest_commands::save(&ctx, &save_dto) {
                Ok(()) => {
                    log::info!("Manifest saved successfully");
                    if let Some(app) = app_weak.upgrade() {
                        app.global::<AppState>()
                            .set_error_message(slint::SharedString::from(""));
                        app.global::<AppState>().set_manifest_is_saved(true);
                        app.global::<AppState>()
                            .set_manifest_path(slint::SharedString::from(save_dto.manifest_path));

                        // Show success message
                        app.global::<AppState>()
                            .set_success_message(slint::SharedString::from(
                                "Manifest saved successfully",
                            ));
                        app.global::<AppState>().set_success_message_visible(true);

                        // Hide after 3 seconds
                        let app_weak_timer = app.as_weak();
                        Timer::single_shot(std::time::Duration::from_secs(3), move || {
                            if let Some(app) = app_weak_timer.upgrade() {
                                app.global::<AppState>().set_success_message_visible(false);
                            }
                        });
                    }
                }
                Err(e) => {
                    log::error!("Failed to save manifest: {}", e);
                    if let Some(app) = app_weak.upgrade() {
                        app.global::<AppState>()
                            .set_error_message(slint::SharedString::from(e));
                    }
                }
            }
        }
    });
}

/// Wire up the on_close_manifest callback
pub fn setup_close_manifest_callback(app: &App, app_context: &Arc<AppContext>) {
    app.global::<ManifestCommands>().on_close_manifest({
        let ctx = Arc::clone(app_context);
        let app_weak = app.as_weak();
        move || {
            log::info!("Close Manifest clicked");
            match handling_manifest_commands::close(&ctx) {
                Ok(()) => {
                    log::info!("Manifest closed successfully");
                    if let Some(app) = app_weak.upgrade() {
                        app.global::<AppState>().set_manifest_is_saved(true);
                        app.global::<AppState>().set_manifest_is_open(false);
                    }
                }
                Err(e) => {
                    log::error!("Failed to close manifest: {}", e);
                }
            }
        }
    });
}

/// Wire up the on_open_qleany_manifest callback
pub fn setup_open_qleany_manifest_callback(app: &App, app_context: &Arc<AppContext>) {
    app.global::<ManifestCommands>().on_open_qleany_manifest({
        let ctx = Arc::clone(app_context);
        let app_weak = app.as_weak();
        move || {
            log::info!("Open Qleany Manifest clicked");
            if let Some(app) = app_weak.upgrade() {
                if app.global::<AppState>().get_manifest_is_open() {
                    log::info!("A manifest is already open, closing it first");

                    // Close any currently open manifest first
                    match handling_manifest_commands::close(&ctx) {
                        Ok(()) => {
                            log::info!("Manifest closed successfully");
                        }
                        Err(e) => {
                            log::error!("Failed to close manifest: {}", e);
                        }
                    }
                }

                // Load the qleany.yaml from the project workspace
                let load_dto = LoadDto {
                    manifest_path: "qleany.yaml".to_string(),
                };
                match handling_manifest_commands::load(&ctx, &load_dto) {
                    Ok(result) => {
                        log::info!("Qleany manifest loaded: {:?}", result);
                        // clear any previous error
                        app.global::<AppState>()
                            .set_error_message(slint::SharedString::from(""));

                        // set workspace_id
                        app.global::<AppState>()
                            .set_workspace_id(result.workspace_id as i32);

                        // set loading
                        app.global::<AppState>().set_is_loading(true);
                        Timer::single_shot(std::time::Duration::from_millis(100), move || {
                            app.global::<AppState>().set_manifest_is_saved(true);
                            app.global::<AppState>().set_is_loading(false);
                            app.global::<AppState>().set_manifest_is_open(true);
                            app.global::<AppState>()
                                .set_manifest_path(slint::SharedString::from("qleany.yaml"));
                        });
                    }
                    Err(e) => {
                        log::error!("Failed to load qleany manifest: {}", e);
                        if let Some(app) = app_weak.upgrade() {
                            app.global::<AppState>()
                                .set_error_message(slint::SharedString::from(e));
                        }
                    }
                }
            }
        }
    });
}

/// Initialize all home tab related callbacks
pub fn init(event_hub_client: &EventHubClient, app: &App, app_context: &Arc<AppContext>) {
    subscribe_loaded_event(event_hub_client, app, app_context);
    subscribe_closed_event(event_hub_client, app, app_context);
    subscribe_new_manifest_event(event_hub_client, app, app_context);
    setup_new_manifest_callback(app, app_context);
    setup_create_wizard_confirmed_callback(app, app_context);
    setup_open_manifest_callback(app, app_context);
    setup_save_manifest_callback(app, app_context);
    setup_save_manifest_as_callback(app, app_context);
    setup_close_manifest_callback(app, app_context);
    setup_open_qleany_manifest_callback(app, app_context);
}