qtrs 0.5.4

qtrs - A type-safe, builder-pattern-driven Qt6 GUI library for Rust
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
//! Qt Settings — Cross-platform persistent application settings
//!
//! QSettings is an abstraction over the system registry, property list files,
//! and INI files, allowing you to save and restore application settings in a
//! portable way.
//!
//! # Examples
//! ```
//! use qtrs::Settings;
//!
//! let settings = Settings::new("MyCompany", "MyApp");
//! settings.set("theme", "dark");
//! assert_eq!(
//!     settings.get("theme", "light")
//!         .convert::<String>(),
//!     Some("dark".to_string())
//! );
//! ```

use crate::ffi::{QSettings, ffi_inner};
use crate::Variant;

// ============================================================
// Type Definitions
// ============================================================

/// Settings scope: user-specific or system-wide
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Scope {
    /// Settings for the current user
    User = 0,
    /// Settings shared by all users on the system
    System = 1,
}

/// Storage format
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Format {
    /// Platform-native format (Windows: registry, macOS: plist, Linux: INI)
    Native = 0,
    /// INI file format
    Ini = 1,
    /// 32-bit registry (Windows only)
    Registry32 = 2,
    /// 64-bit registry (Windows only)
    Registry64 = 3,
}

/// Settings status code
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Status {
    /// No error occurred
    NoError = 0,
    /// Access error (e.g., read-only file)
    AccessError = 1,
    /// Format error (e.g., malformed INI file)
    FormatError = 2,
}

// ============================================================
// Settings
// ============================================================

/// Rust wrapper for QSettings
///
/// Provides cross-platform persistent configuration management.
///
/// # Lifetimes
/// When `Settings` is dropped, `sync()` is automatically called to flush
/// changes to disk.
pub struct Settings {
    inner: *mut QSettings,
}

unsafe impl Send for Settings {}
unsafe impl Sync for Settings {}

impl Settings {
    // ============================================================
    // Constructors
    // ============================================================

    /// Creates a new settings object (user scope, native format)
    ///
    /// # Arguments
    /// - `organization`: Organization name (e.g., "MyCompany")
    /// - `application`: Application name (e.g., "MyApp")
    ///
    /// # Examples
    /// ```
    /// use qtrs::Settings;
    /// let settings = Settings::new("MySoft", "StarRunner");
    /// ```
    pub fn new(organization: &str, application: &str) -> Self {
        unsafe {
            Self {
                inner: ffi_inner::QSettings_new_user_app(&organization.to_string(), &application.to_string()),
            }
        }
    }

    /// Creates a settings object with the specified scope
    ///
    /// # Arguments
    /// - `scope`: `Scope::User` or `Scope::System`
    /// - `organization`: Organization name
    /// - `application`: Application name
    ///
    /// # Examples
    /// ```
    /// use qtrs::{Settings, Scope};
    /// let settings = Settings::new_with_scope(Scope::System, "MyCompany", "MyApp");
    /// ```
    pub fn new_with_scope(scope: Scope, organization: &str, application: &str) -> Self {
        unsafe {
            Self {
                inner: ffi_inner::QSettings_new_scope_app(scope as i32, &organization.to_string(), &application.to_string()),
            }
        }
    }

    /// Creates a settings object with the specified format and scope
    ///
    /// # Arguments
    /// - `format`: Storage format (`Format::Native` or `Format::Ini`)
    /// - `scope`: Scope (`Scope::User` or `Scope::System`)
    /// - `organization`: Organization name
    /// - `application`: Application name
    ///
    /// # Examples
    /// ```
    /// use qtrs::{Settings, Format, Scope};
    /// let settings = Settings::new_with_format(Format::Ini, Scope::User, "MyCompany", "MyApp");
    /// ```
    pub fn new_with_format(format: Format, scope: Scope, organization: &str, application: &str) -> Self {
        unsafe {
            Self {
                inner: ffi_inner::QSettings_new_format_scope(format as i32, scope as i32, &organization.to_string(), &application.to_string()),
            }
        }
    }

    /// Creates a settings object backed by a specific file
    ///
    /// # Arguments
    /// - `path`: File path
    /// - `format`: File format (usually `Format::Ini`)
    ///
    /// # Examples
    /// ```
    /// use qtrs::{Settings, Format};
    /// let settings = Settings::new_from_file("/etc/myapp.conf", Format::Ini);
    /// ```
    pub fn new_from_file(path: &str, format: Format) -> Self {
        unsafe {
            Self {
                inner: ffi_inner::QSettings_new_file(&path.to_string(), format as i32),
            }
        }
    }

    // ============================================================
    // Basic Read/Write
    // ============================================================

    /// Writes a setting
    ///
    /// # Arguments
    /// - `key`: Key name (supports `/` hierarchy, e.g., `"mainwindow/size"`)
    /// - `value`: String value
    ///
    /// # Examples
    /// ```
    /// # use qtrs::Settings;
    /// # let settings = Settings::new("MyCompany", "MyApp");
    /// settings.set("theme", "dark");
    /// ```
    pub fn set<T: Into<Variant>>(&self, key: &str, value: T) {
        let variant = value.into();
        unsafe {
            ffi_inner::QSettings_setValue(self.inner, &key.to_string(), variant.raw_ptr());
        }
    }

    /// Reads a setting
    ///
    /// # Arguments
    /// - `key`: Key name
    /// - `default`: Default value returned if the key does not exist
    ///
    /// # Returns
    /// The setting value, or `default` if the key is not found
    ///
    /// # Examples
    /// ```
    /// # use qtrs::Settings;
    /// # let settings = Settings::new("MyCompany", "MyApp");
    /// let theme = settings.get("theme", "light");
    /// ```
    pub fn get<T: Into<Variant>>(&self, key: &str, default: T) -> Variant {
        let default_value = default.into();
        unsafe {
            Variant {
                inner: ffi_inner::QSettings_value(
                    self.inner,
                    &key.to_string(),
                    default_value.raw_ptr()
                )
            }
        }
    }

    // ============================================================
    // Group Operations
    // ============================================================

    /// Begins a group
    ///
    /// All keys within the group are automatically prefixed.
    ///
    /// # Examples
    /// ```
    /// # use qtrs::Settings;
    /// # let settings = Settings::new("MyCompany", "MyApp");
    /// settings.begin_group("mainwindow");
    /// settings.set("size", "1024x768");  // Actual key: "mainwindow/size"
    /// settings.end_group();
    /// ```
    pub fn begin_group(&self, prefix: &str) {
        unsafe {
            ffi_inner::QSettings_beginGroup(self.inner, &prefix.to_string());
        }
    }

    /// Ends the current group
    pub fn end_group(&self) {
        unsafe {
            ffi_inner::QSettings_endGroup(self.inner);
        }
    }

    /// Returns the current group path
    pub fn group(&self) -> String {
        unsafe {
            ffi_inner::QSettings_group(self.inner)
        }
    }

    // ============================================================
    // Array Operations
    // ============================================================

    /// Begins reading an array
    ///
    /// Returns the array size. Must be used with `set_array_index()` and `end_array()`.
    ///
    /// # Examples
    /// ```
    /// # use qtrs::Settings;
    /// # let settings = Settings::new("MyCompany", "MyApp");
    /// let size = settings.begin_read_array("logins");
    /// for i in 0..size {
    ///     settings.set_array_index(i);
    ///     let name = settings.get("username", "");
    /// }
    /// settings.end_array();
    /// ```
    pub fn begin_read_array(&self, prefix: &str) -> i32 {
        unsafe {
            ffi_inner::QSettings_beginReadArray(self.inner, &prefix.to_string())
        }
    }

    /// Begins writing an array
    ///
    /// # Arguments
    /// - `prefix`: Array prefix
    /// - `size`: Array size (-1 for auto-detection)
    pub fn begin_write_array(&self, prefix: &str, size: i32) {
        unsafe {
            ffi_inner::QSettings_beginWriteArray(self.inner, &prefix.to_string(), size);
        }
    }

    /// Ends the current array
    pub fn end_array(&self) {
        unsafe {
            ffi_inner::QSettings_endArray(self.inner);
        }
    }

    /// Sets the current array index
    ///
    /// Must be called after `begin_read_array()` or `begin_write_array()`.
    pub fn set_array_index(&self, index: i32) {
        unsafe {
            ffi_inner::QSettings_setArrayIndex(self.inner, index);
        }
    }

    // ============================================================
    // Query Operations
    // ============================================================

    /// Checks whether a key exists
    pub fn contains(&self, key: &str) -> bool {
        unsafe {
            ffi_inner::QSettings_contains(self.inner, &key.to_string())
        }
    }

    /// Removes the specified key
    pub fn remove(&self, key: &str) {
        unsafe {
            ffi_inner::QSettings_remove(self.inner, &key.to_string());
        }
    }

    /// Removes all keys in the current group
    ///
    /// # Examples
    /// ```
    /// # use qtrs::Settings;
    /// # let settings = Settings::new("MyCompany", "MyApp");
    /// settings.begin_group("cache");
    /// settings.clear();  // Only removes keys under "cache"
    /// settings.end_group();
    /// ```
    pub fn clear(&self) {
        unsafe {
            ffi_inner::QSettings_clear(self.inner);
        }
    }

    /// Returns all keys
    pub fn all_keys(&self) -> Vec<String> {
        unsafe {
            ffi_inner::QSettings_allKeys(self.inner)
        }
    }

    /// Returns child keys in the current group
    pub fn child_keys(&self) -> Vec<String> {
        unsafe {
            ffi_inner::QSettings_childKeys(self.inner)
        }
    }

    /// Returns child groups in the current group
    pub fn child_groups(&self) -> Vec<String> {
        unsafe {
            ffi_inner::QSettings_childGroups(self.inner)
        }
    }

    // ============================================================
    // Status and Sync
    // ============================================================

    /// Syncs to disk
    ///
    /// Writes all unsaved changes to permanent storage.
    /// The destructor automatically calls this method, so manual calls
    /// are usually not necessary.
    pub fn sync(&self) {
        unsafe {
            ffi_inner::QSettings_sync(self.inner);
        }
    }

    /// Returns whether the settings are writable
    pub fn is_writable(&self) -> bool {
        unsafe {
            ffi_inner::QSettings_isWritable(self.inner)
        }
    }

    /// Returns the current status
    pub fn status(&self) -> Status {
        unsafe {
            match ffi_inner::QSettings_status(self.inner) {
                0 => Status::NoError,
                1 => Status::AccessError,
                2 => Status::FormatError,
                _ => Status::NoError,
            }
        }
    }

    /// Returns the file path / registry path
    pub fn file_name(&self) -> String {
        unsafe {
            ffi_inner::QSettings_fileName(self.inner)
        }
    }

    /// Returns whether fallbacks are enabled
    pub fn fallbacks_enabled(&self) -> bool {
        unsafe {
            ffi_inner::QSettings_fallbacksEnabled(self.inner)
        }
    }

    /// Enables or disables fallbacks
    ///
    /// When disabled, only reads from the current scope and does not
    /// search fallback locations.
    pub fn set_fallbacks_enabled(&self, enabled: bool) {
        unsafe {
            ffi_inner::QSettings_setFallbacksEnabled(self.inner, enabled);
        }
    }

    /// Returns the raw pointer (for advanced use)
    pub fn as_ptr(&self) -> *mut QSettings {
        self.inner
    }
}

// ============================================================
// Drop
// ============================================================

impl Drop for Settings {
    /// Automatically syncs and frees resources on drop
    fn drop(&mut self) {
        if !self.inner.is_null() {
            unsafe {
                ffi_inner::QSettings_sync(self.inner);
                ffi_inner::QSettings_delete(self.inner);
            }
        }
    }
}

// ============================================================
// Debug & Display
// ============================================================

impl std::fmt::Debug for Settings {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let keys = self.all_keys();
        f.debug_struct("Settings")
            .field("file", &self.file_name())
            .field("key_count", &keys.len())
            .field("keys", &keys)
            .finish()
    }
}

impl std::fmt::Display for Settings {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Settings({} keys, file: {})", 
               self.all_keys().len(), 
               self.file_name())
    }
}

// ============================================================
// Tests
// ============================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_basic_operations() {
        let settings = Settings::new("TestOrg", "TestApp");
        settings.set("key", "value");
        assert_eq!(settings.get("key", ""), Variant::from("value"));
        settings.remove("key");
        assert!(!settings.contains("key"));
    }

    #[test]
    fn test_group() {
        let settings = Settings::new("TestOrg", "TestApp");
        settings.begin_group("group");
        settings.set("key", "value");
        settings.end_group();
        assert_eq!(
            settings.get("group/key", "")
                .convert::<String>()
                .unwrap(),
            "value"
        );
    }
}