@@ -9,7 +9,9 @@ mod iter;
9
9
pub use self :: iter:: Iter ;
10
10
use std:: borrow:: Cow ;
11
11
use std:: collections:: BTreeMap ;
12
+
12
13
use std:: path:: { Path , PathBuf } ;
14
+ use xdg:: BaseDirectories ;
13
15
14
16
pub type Group < ' a > = & ' a str ;
15
17
pub type Groups < ' a > = BTreeMap < Group < ' a > , KeyMap < ' a > > ;
@@ -279,42 +281,53 @@ pub enum PathSource {
279
281
LocalNix ,
280
282
Nix ,
281
283
System ,
284
+ SystemLocal ,
282
285
SystemFlatpak ,
283
286
SystemSnap ,
284
287
Other ( String ) ,
285
288
}
286
289
287
- pub fn default_paths ( ) -> Vec < ( PathSource , PathBuf ) > {
288
- let home_dir = dirs:: home_dir ( ) . unwrap ( ) ;
289
-
290
- vec ! [
291
- ( PathSource :: LocalDesktop , home_dir. join( "Desktop" ) ) ,
292
- (
293
- PathSource :: LocalFlatpak ,
294
- home_dir. join( ".local/share/flatpak/exports/share/applications" ) ,
295
- ) ,
296
- (
297
- PathSource :: Local ,
298
- home_dir. join( ".local/share/applications" ) ,
299
- ) ,
300
- (
301
- PathSource :: LocalNix ,
302
- home_dir. join( ".nix-profile/share/applications" ) ,
303
- ) ,
304
- (
305
- PathSource :: SystemSnap ,
306
- PathBuf :: from( "/var/lib/snapd/desktop/applications" ) ,
307
- ) ,
308
- (
309
- PathSource :: SystemFlatpak ,
310
- PathBuf :: from( "/var/lib/flatpak/exports/share/applications" ) ,
311
- ) ,
312
- (
313
- PathSource :: Nix ,
314
- PathBuf :: from( "/nix/var/nix/profiles/default/share/applications" ) ,
315
- ) ,
316
- ( PathSource :: System , PathBuf :: from( "/usr/share/applications" ) ) ,
317
- ]
290
+ impl PathSource {
291
+
292
+ /// Attempts to determine the PathSource for a given Path.
293
+ /// Note that this is a best-effort guesting function, and its results should be treated as
294
+ /// such (e.g.: non-canonical).
295
+ pub fn guess_from ( path : & Path ) -> PathSource {
296
+ let base_dirs = BaseDirectories :: new ( ) . unwrap ( ) ;
297
+ let data_home = base_dirs. get_data_home ( ) ;
298
+
299
+ if path. starts_with ( "/usr/share" ) {
300
+ PathSource :: System
301
+ } else if path. starts_with ( "/usr/local/share" ) {
302
+ PathSource :: SystemLocal
303
+ } else if path. starts_with ( "/var/lib/flatpak" ) {
304
+ PathSource :: SystemFlatpak
305
+ } else if path. starts_with ( "/var/lib/snapd" ) {
306
+ PathSource :: SystemSnap
307
+ } else if path. starts_with ( "/nix/var/nix/profiles/default" ) || path. starts_with ( "/nix/store" ) {
308
+ PathSource :: Nix
309
+ } else if path. to_string_lossy ( ) . contains ( "/flatpak/" ) {
310
+ PathSource :: LocalFlatpak
311
+ } else if path. starts_with ( & data_home. as_path ( ) ) {
312
+ PathSource :: Local
313
+ } else if path. starts_with ( "/nix/var/nix/profiles/per-user" ) || path. to_string_lossy ( ) . contains ( ".nix" ) {
314
+ PathSource :: LocalNix
315
+ } else {
316
+ PathSource :: Other ( String :: from ( "unknown" ) )
317
+ }
318
+ }
319
+ }
320
+
321
+ /// Returns the default paths in which desktop entries should be searched for based on the current
322
+ /// environment.
323
+ ///
324
+ /// Panics in case determining the current home directory fails.
325
+ pub fn default_paths ( ) -> Vec < PathBuf > {
326
+ let base_dirs = BaseDirectories :: new ( ) . unwrap ( ) ;
327
+ let mut data_dirs = base_dirs. get_data_dirs ( ) ;
328
+ data_dirs. push ( base_dirs. get_data_home ( ) ) ;
329
+
330
+ data_dirs. iter ( ) . map ( |d| d. join ( "applications" ) ) . collect ( )
318
331
}
319
332
320
333
fn dgettext ( domain : & str , message : & str ) -> String {
0 commit comments