xdgkit 3.2.5

A command line kit for XDG specs. As well as a Rust library
Documentation
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
592
593
594
595
596
597
598
599
600
601
602
603
/*!
 * # Icon finder
 *
 * This is the rustification of the example psuedo code for finding icons a.k.a "the algorithm described in the [Icon Theme Specification](https://specifications.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html#icon_lookup)"
 *
 * to find one single icon use:
 * ```
 * use xdgkit::icon_finder::find_icon;
 * let icon = find_icon("firefox".to_string(), 48, 1);
 * ```
 * to find multiple icons use:
 * ```
 * use xdgkit::icon_theme::IconTheme;
 * use xdgkit::icon_finder::{multiple_find_icon, generate_dir_list, user_theme, DirList};
 *
 * let dir_list_vector = generate_dir_list();
 * let mut theme = user_theme(dir_list_vector.clone());
 * if theme.is_none() {
 *    theme = Some(IconTheme::empty());
 * }
 * let theme:IconTheme = theme.unwrap();
 * let list:Vec<String> = vec![
 *    "firefox".to_string(),
 *    "mypaint".to_string(),
 *    "kate".to_string(),
 *    "geany".to_string(),
 * ];
 * for name in list.clone() {
 *    let icon = match multiple_find_icon(name.clone(), 48, 1, dir_list_vector.clone(), theme.clone()) {
 *        Some(i) => i,
 *        None => continue,
 *    };
 *    let icon = match icon.to_str() {
 *        Some(i) => {
 *            println!("found:{}", i);
 *            i
 *        },
 *        None => {
 *            println!("Did not find:{}", name.as_str());
 *            continue
 *        },
 *    };
 * }
 */

// icon_finder.rs
// Rusified in 2021 Copyright Israel Dahl. All rights reserved.
//
//        /VVVV\
//      /V      V\
//    /V          V\
//   /      0 0     \
//   \|\|\</\/\>/|/|/
//        \_/\_/
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

use crate::icon_theme::*;
use crate::basedir::*;
use std::path::PathBuf;
use std::path::Path;
extern crate tini;
use tini::Ini;
use std::collections::HashMap;

/// Our icon file extensions
const EXTENTIONS:[&str; 3] = [".png", ".svg", ".xpm"];
/// the index file for themes
const INDEX_FILE:&str = "index.theme";

/// A simple structure to hold directory and theme list.
///
/// The reason this is needed is because the **theme** name is **not** the same as the **directory name** `:-P`
#[derive(Debug, Clone)]
pub struct DirList {
    /// the PathBuf
    pub dirs:Vec<PathBuf>,
    /// The name of the theme
    pub theme:String,
}
impl DirList {
    /// In a nutshell, return `dir.push("/index.theme")`
    pub fn index(&self) -> PathBuf {
        let mut return_value = self.dirs[0].to_owned();
        return_value.push(INDEX_FILE);
        return_value
    }
}
/// Make the list of `DirList` structures by reading the `$XDG_DATA_DIRS/icons`
pub fn generate_dir_list() -> Vec<DirList>{
    let mut return_value:Vec< DirList> = vec![];

    // make our directory of icons
    let directory_vec = icon_dirs_vector();
    //println!("search");

    let mut themes = HashMap::<String, Vec<PathBuf>>::new(); // theme dirname -> path to topmost directory of the theme
    // find all theme dir basenames, put those in a hashmap, then make a DirList each to traverse all of the directories that are named the same.
    for directory in directory_vec {
        let path = Path::new(directory.as_str());
        if path.is_dir() {
            let dir_path = std::fs::read_dir(path);
            if let Ok(dp) = dir_path {
                for entity in dp.flatten() {
                    //println!("entity:{:?}", entity.file_name());
                    let return_path = entity.path();
                    let basename = entity.file_name();
                    let theme_name = match basename.to_str() {
                        Some(v) => v,
                        None => continue
                    };
                    themes.entry(theme_name.to_string()).and_modify(|paths| paths.push(return_path.clone())).or_insert(Vec::<PathBuf>::from([return_path.clone()]));
                }
            }
        }
    }

    for (theme_name, ref mut return_paths) in themes {
        let mut index_theme_path = None;
        let mut index_theme_index = 0;
        for (i, return_path) in return_paths.iter().enumerate() {
            let mut index = return_path.clone();
            index.push(INDEX_FILE);
            if index.is_file() {
                index_theme_path = Some(index);
                index_theme_index = i;
            }
        }
        if index_theme_path.is_none() {
            continue
        }
        // make sure that the entry with index.theme in it is in front
        return_paths.swap(0, index_theme_index);
        let index_theme_path = index_theme_path.unwrap();
        let theme = IconTheme::new(index_theme_path.to_owned().to_str().unwrap().to_string());
        let theme_name = match theme.name {
            Some(name) => name.to_owned(),
            None => continue,
        };
        //println!("index file:{}", index_theme_path.display());
        //println!(" dirs: {:?}", return_paths);
        //println!("{:?}",index.to_owned());
        return_value.push(
            DirList{
                theme:theme_name.to_owned(),
                          dirs:return_paths.clone(),
            }
        );
    }
    //return_value.sort();
    return_value
}

fn get_theme(place: &str, dir_list_vector:Vec<DirList>) -> Option<DirList> {
    for theme in &dir_list_vector {
        if theme.theme == place {
            return Some(theme.clone())
        }
    }
    // maybe that's actually trying to find PLACE as a filename? That sounds weird.
    for theme in dir_list_vector {
        for dir in theme.dirs.iter() {
            if let Some(file_name) = dir.file_name() {
                if file_name == place {
                    return Some(theme.clone())
                }
            }
        }
    }
    return None
}

fn check_user_config(file:&str, section:&str, item:&str, dir_list_vector:Vec<DirList>) -> Option<IconTheme> {
    let conf:String = match config_home() {
        Ok(conf) => format!("{}/{}",conf, file),
        Err(e) => {
            println!("Error:{}", e);
            return None
        },
    };
    if Path::new(conf.as_str()).is_file() {
        let test_ini = Ini::from_file(&conf);
        if let Ok(conf) = test_ini {
            let theme:Option<String> = conf.get(section,item);
            //println!("{:?}", theme.clone());
            if let Some(themed) = theme {
                let theme_file:PathBuf = match get_theme(
                    &themed,
                    dir_list_vector.
                    clone()
                ).map(|theme| theme.index()) {
                    Some(theme_file) =>theme_file,
                    None =>PathBuf::new(),
                };
                if theme_file.exists() {
                    return Some(IconTheme::from_pathbuff(theme_file));
                }
            }
        }
    }
    None
}
/// This function looks in the ini files of KDE and GTK to find the icon theme!
pub fn user_theme(dir_list_vector:Vec<DirList>) -> Option<IconTheme> {
    let theme_dir = icon_dirs();
    if theme_dir.is_ok() {
        // let us look for the user icon theme now that we have directories to look in
        if let Some(kde) = check_user_config("kdeglobals",
            "Icons",
            "Theme",
            dir_list_vector.clone()) {
            return Some(kde);
            };
        if let Some(gtk) = check_user_config("gtk-3.0/settings.ini",
            "Settings",
            "gtk-icon-theme-name",
            dir_list_vector.clone()) {
            return Some(gtk);
            };
        if let Some(gtk) = check_user_config("gtk-4.0/settings.ini",
            "Settings",
            "gtk-icon-theme-name",
            dir_list_vector.clone()) {
            return Some(gtk);
            };
    }
    // Default to `hicolor` theme if we can't figure it out
    let theme_file:PathBuf = match get_theme("hicolor", dir_list_vector).map(|theme| theme.index()) {
        Some(theme_file) =>theme_file,
        None =>PathBuf::new(),
    };
    if theme_file.exists() {
        return Some(IconTheme::from_pathbuff(theme_file))
    }
    None
}

/// # Icon Lookup
///
/// The icon lookup mechanism has two global settings, the list of base directories and the internal name of the current theme. Given these we need to specify how to look up an icon file from the icon name, the nominal size and the scale.
///
/// The lookup is done first in the current theme, and then recursively in each of the current theme's parents, and finally in the default theme called "hicolor" (implementations may add more default themes before "hicolor", but "hicolor" must be last). As soon as there is an icon of any size that matches in a theme, the search is stopped. Even if there may be an icon with a size closer to the correct one in an inherited theme, we don't want to use it. Doing so may generate an inconsistent change in an icon when you change icon sizes (e.g. zoom in).
///
/// The lookup inside a theme is done in three phases. First all the directories are scanned for an exact match, e.g. one where the allowed size of the icon files match what was looked up. Then all the directories are scanned for any icon that matches the name. If that fails we finally fall back on un-themed icons. If we fail to find any icon at all it is up to the application to pick a good fallback, as the correct choice depends on the context.
///
/// The exact algorithm (in rust) is now here:
// this is our main function used by main.rs to find a single 'named' icon, regardless of type
pub fn find_icon(icon:String, size:i32, scale:i32) -> Option<PathBuf> {
    let dir_list_vector = generate_dir_list();
    let mut theme = user_theme(dir_list_vector.clone());
    if theme.is_none() {
        //println!("No user theme");
        theme = Some(IconTheme::empty());
    }
    let theme:IconTheme = theme.unwrap();
    //println!("theme:{}", theme.clone().name.unwrap().as_str());
    multiple_find_icon(icon, size, scale, dir_list_vector, theme)
}
pub fn multiple_find_icon(icon:String, size:i32, scale:i32, dir_list_vector:Vec<DirList>, theme:IconTheme) -> Option<PathBuf> {
    // try with the default theme
    let mut filename:Option<PathBuf> = find_icon_helper(icon.to_owned(), size, scale, theme, dir_list_vector.clone());
    if filename.is_some(){ return filename }

    // check hi-color a.k.a the "default" theme
    let theme_file:PathBuf = match get_theme("hicolor", dir_list_vector.clone()).map(|theme| theme.index()) {
        Some(theme_file) => theme_file,
        None => PathBuf::new(),
    };
    let i_theme_file:String = match theme_file.as_path().to_str() {
        Some(t) => String::from(t),
        None => String::from(""),
    };
    let hicolor = IconTheme::new(i_theme_file);
    filename = find_icon_helper(icon.to_owned(), size, scale, hicolor, dir_list_vector);
    if filename.is_some(){ return filename }

    // just find something already....
    lookup_fallback_icon(icon)
}

/// the "helper" function from the free desktop example pseudo code
pub fn find_icon_helper(icon:String, size:i32, scale:i32, theme:IconTheme, dir_list_vector:Vec<DirList>) -> Option<PathBuf> {
    let mut filename:Option<PathBuf> = lookup_icon (icon.to_owned(), size, scale, theme.clone(), dir_list_vector.clone());
    if filename.is_some(){ return filename }

    if let Some(parents) = theme.inherits {
        for parent in parents {
            // make a theme from the 'parent'
            let theme_file:PathBuf = match get_theme(&parent, dir_list_vector.clone()).map(|theme| theme.index()) {
                Some(theme_file) => theme_file,
                None => PathBuf::new(),
            };
            let i_theme_file:String = match theme_file.as_path().to_str() {
                Some(t) => String::from(t),
                None => String::from(""),
            };
            let parent_theme = IconTheme::new(i_theme_file.to_owned());
            // boo recursion :(
            filename = find_icon_helper (icon.to_owned(), size, scale, parent_theme.clone(), dir_list_vector.clone());
            if filename.is_some(){ return filename }
        }
    }
    None
}

/// One of the "following helper functions"
pub fn lookup_icon (iconname:String, size:i32, scale:i32, theme:IconTheme, dir_list_vector:Vec<DirList>) -> Option<PathBuf> {
    let list = theme.directories.to_owned();
    //eprintln!("LIST {:?} {:?}", theme, list);
    match list.as_ref() {
        Some(_r) => (),
        None => {
            println!("Could not turn list into reference");
            return None;
        },
    };

    let theme_name = theme.name
    .unwrap();
    let mut closest_filename:PathBuf = PathBuf::new();
    let theme_subdir_list:Vec<Directory> = list.unwrap();

    if let Some(theme) = get_theme(&theme_name, dir_list_vector) {
        for directory in theme.dirs {
            // first look check for size matching directories
            for subdir in theme_subdir_list.clone() {
                let subdir_name = subdir.name
                .to_owned()
                .unwrap();
                for extension in EXTENTIONS.iter() {
                    let mut path = directory.to_owned();
                    path.push(subdir_name.as_str());
                    let mut file_name:String = iconname.to_owned();
                    file_name.push_str(extension);
                    path.push(file_name.as_str());
                    //println!("{:?} exists:{:?}", path, path.as_path().is_file());
                    if directory_matches_size(subdir.to_owned(), size, scale)
                        && path.as_path().is_file() {
                            return Some(path)
                        }
                }
            }
        }
    }
    // ok second try lets look through all of them
    let mut minimal_size:i32 = std::i32::MAX;
    //TODO
    for subdir in theme_subdir_list {
        let subdir_name = subdir.name
        .to_owned()
        .unwrap();
        let directory_vec:Vec<PathBuf> = to_pathbuff(icon_dirs_vector());
        for directory in directory_vec {
            for extension in EXTENTIONS.iter() {
                let mut path = directory.to_owned();
                path.push(theme_name.as_str());
                path.push(subdir_name.as_str());
                let mut file_name:String = iconname.to_owned();
                file_name.push_str(extension);
                path.push(file_name.as_str());
                if path.as_path().is_file() && directory_size_distance(subdir.clone(), size, scale) < minimal_size {
                    closest_filename = path.to_owned();
                    minimal_size = directory_size_distance(subdir.clone(), size, scale);
                }
            }
        }
    }
    if closest_filename.as_path() == Path::new("") {
        None
    } else {
        Some(closest_filename)
    }
}

/// Look in the basic icon directories (like /us/share/pixmaps, /usr/share/icons) for anything that matches the icon name!
pub fn lookup_fallback_icon (iconname:String) ->Option<PathBuf> {
    let directory_vec:Vec<PathBuf> = to_pathbuff(icon_dirs_vector());
    for directory in directory_vec {
        for extension in EXTENTIONS.iter() {
            let mut path = directory.to_owned();
            let mut file_name:String = iconname.to_owned();
            file_name.push_str(extension);
            path.push(file_name.as_str());
            if path.as_path().is_file() {
                return Some(path)
            }
        }
    }
    None
}

/// Check to see if the sub directory size is in range
pub fn directory_matches_size(subdir:Directory, iconsize:i32, iconscale:i32) -> bool {
    let mut scale = 1;
    if subdir.scale.is_some() {
        scale = subdir.scale.unwrap();
    }
    // check scale sent in
    if scale != iconscale {
        // wrong scale
        return false
    }
    // get our variables
    let mut d_type = DirectoryType::Threshold;
    if let Some(d) = subdir.xdg_type {
        d_type = d;
    }
    let size = subdir.size;
    // need a default size to check against below
    if size.is_none() {
        return false
    }
    let size:i32  = size.unwrap();
    //println!("DirectoryType:{:?}, input size:{} scale:{} vs size:{} scale:{}", d_type, iconsize, iconscale, size, scale);
    // do we have a minimum?
    let min_size:i32 = match subdir.min_size {
        Some(s) => s,
        None => size,
    };
    // do we have a maximum?
    let max_size:i32  = match subdir.max_size {
        Some(s) => s,
        None => size,
    };
    // do we have a threshold?
    let threshold:i32 = match subdir.threshold {
        Some(s) => s,
        None => 2,
    };
    // what type of directory setting do we have?
    match d_type {
        DirectoryType::Fixed => {
            // is it fixed?
            return size == iconsize
        },
        DirectoryType::Scalable => {
            // if it scales okay
            if min_size <= iconsize &&
                iconsize <= max_size {
                    //println!("scalable");
                    return true
                }
        },
        DirectoryType::Threshold => {
            // is this in the threshold?
            if (size - threshold) <= iconsize &&
                iconsize <= (size + threshold) {
                    //println!("in threshold");
                    return true
                }
        }
    }
    // didn't match the icon parameters for this directory in this directory
    false
}
/// You guessed it more pseudo code that turned into rust
pub fn directory_size_distance(subdir:Directory, iconsize:i32, iconscale:i32) -> i32{
    // default scale is 1
    let mut scale = 1;
    if subdir.scale.is_some() {
        scale = subdir.scale.unwrap();
    }
    // default type is "Threshold"
    let mut d_type = DirectoryType::Threshold;
    if subdir.xdg_type.is_some() {
        d_type = subdir.xdg_type.unwrap();
    }
    // we need the size for the defaults later
    let size = subdir.size;
    if size.is_none() {return 0}
    let size:i32  = size.unwrap();
    // default to sie
    let mut min_size:i32  = subdir.size.unwrap();
    if subdir.min_size.is_some() {
        min_size = subdir.min_size.unwrap();
    }
    // efault to size
    let mut max_size:i32  = subdir.size.unwrap();
    if subdir.max_size.is_some() {
        max_size = subdir.max_size.unwrap();
    }
    // 2 is the default "threshold"
    let mut threshold:i32 = 2;
    if subdir.threshold.is_some() {
        threshold = subdir.threshold.unwrap();
    }

    // now we check our Directory "type"
    // all this math came directly from the page and is edited to be compatible with Rust
    match d_type {
        DirectoryType::Fixed => {
            let num:i32 = size * scale - iconsize * iconscale;
            num.abs()
        },
        DirectoryType::Scalable => {
            if iconsize * iconscale < min_size * scale {
                return min_size * scale - iconsize * iconscale
            }
            if iconsize * iconscale > max_size * scale {
                return iconsize * iconscale - max_size * scale
            }
            0
        },
        DirectoryType::Threshold => {
            if iconsize * iconscale < (size - threshold) * scale {
                return min_size * scale - iconsize * iconscale
            }
            if iconsize*iconsize > (size + threshold) * scale {
                return iconsize * iconsize - max_size * scale
            }
            0
        },
    }
}

/// In some cases you don't always want to fall back to an icon in an inherited theme. For instance, sometimes you look for a set of icons, preferring any of them before using an icon from an inherited theme. To support such operations implementations can contain a function that finds the first of a list of icon names in the inheritance hierarchy. This is that function!
pub fn find_best_icon(icon_list:Vec<String>, size:i32, scale:i32) -> Option<PathBuf> {

    let dir_list_vector = generate_dir_list();
    let theme:IconTheme = match user_theme(dir_list_vector.clone()){
        Some(theme) => theme,
        None => {
            IconTheme::empty();
            return None
        },
    };

    // Get the filename?
    let mut filename:Option<PathBuf> = find_best_icon_helper(
        icon_list.clone(),
                                                             size,
                                                             scale,
                                                             theme,
                                                             dir_list_vector.clone()
    );
    if filename.is_some(){ return filename }

    // check hicolor a.k.a the "default theme"

    let theme_file:PathBuf = match get_theme("hicolor", dir_list_vector.clone()).map(|theme| theme.index()) {
        Some(theme_file) => theme_file,
        None => PathBuf::new(),
    };
    let i_theme_file:String = match theme_file.as_path().to_str() {
        Some(t) => String::from(t),
        None => String::from(""),
    };
    let hicolor = IconTheme::new(i_theme_file);
    filename = find_best_icon_helper(icon_list.clone(), size, scale, hicolor, dir_list_vector);

    if filename.is_some(){ return filename }

    for icon in icon_list {
        filename = lookup_fallback_icon(icon);
        if filename.is_some(){ return filename }
    }
    None
}

/// This can be very useful, for example, when handling mime type icons, where there are more and less "specific" versions of icons.
pub fn find_best_icon_helper(icon_list:Vec<String>, size:i32, scale:i32, theme:IconTheme, dir_list_vector:Vec<DirList>) -> Option<PathBuf> {
    let mut filename = None;
    let list = icon_list;
    let other = list.clone();
    // look through a list of names to find any icon that is similar
    for icon in list {
        filename = lookup_icon(icon, size, scale, theme.clone(), dir_list_vector.clone());

        if filename.is_some(){ return filename }
    }

    // check the inherits
    let inherits = theme.inherits;
    if  let Some(parents) = inherits {
        for parent in parents {
            // make a theme from the 'parent'
            let theme_file:PathBuf = match get_theme(&parent, dir_list_vector.clone()).map(|theme| theme.index()) {
                Some(theme_file) => theme_file,
                None => PathBuf::new(),
            };
            let i_theme_file:String = match theme_file.as_path().to_str() {
                Some(t) => String::from(t),
                None => String::from(""),
            };
            let parent_theme = IconTheme::new(i_theme_file);
            filename = find_best_icon_helper(other.clone(), size, scale, parent_theme.clone(), dir_list_vector.clone());

            if filename.is_some(){ return filename }
        }
    }
    filename
}