Example #1
0
 /// <summary>
 /// Given an absolute path to a directory, scan that directory for
 /// .desktop files, creating an ApplicationItem for each desktop file
 /// found and adding the ApplicationItem to the list of
 /// ApplicationItems.
 /// </summary>
 /// <param name="dir">
 /// A <see cref="System.String"/> containing an absolute path to a
 /// directory
 /// where .desktop files can be found.
 /// </param>
 IEnumerable <KeyValuePair <string, ApplicationItem> > LoadDesktopFiles(string dir)
 {
     return(GetDesktopFiles(dir)
            .Where(ShouldUseDesktopFile)
            .Select(f => new KeyValuePair <string, ApplicationItem> (f, ApplicationItem.MaybeCreateFromDesktopItem(f)))
            .Where(a => a.Value != null)
            .Where(a => a.Value.ShouldShow));
 }
Example #2
0
        void OnFileRenamed(object sender, RenamedEventArgs e)
        {
            Item            disappearingItem = null;
            ApplicationItem newItem          = null;

            lock (app_items) {
                if (app_items.ContainsKey(e.OldFullPath))
                {
                    Log <ApplicationItemSource> .Debug("Desktop file {0} moved away", e.OldFullPath);

                    disappearingItem = app_items[e.OldFullPath];
                    app_items.Remove(e.OldFullPath);
                }
                if (e.FullPath.EndsWith(".desktop", StringComparison.Ordinal))
                {
                    Log <ApplicationItemSource> .Debug("Desktop file {0} moved into watched directory", e.FullPath);

                    newItem = ApplicationItem.MaybeCreateFromDesktopItem(e.FullPath);
                    if (newItem == null)
                    {
                        Log.Error("Found new Desktop file {0} but unable to create an item in the Universe", e.FullPath);
                    }
                    else
                    {
                        app_items [e.FullPath] = newItem;
                    }
                }
            }
            if (disappearingItem != null)
            {
                RaiseItemsUnavailable(new ItemsUnavailableEventArgs()
                {
                    unavailableItems = new Item[] { disappearingItem }
                });
            }
            if (newItem != null)
            {
                RaiseItemsAvailable(new ItemsAvailableEventArgs()
                {
                    newItems = new Item[] { newItem }
                });
            }
        }
Example #3
0
        public static ApplicationItem MaybeCreateFromDesktopItem(string path)
        {
            string          key = path;
            ApplicationItem appItem;

            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            lock (Instances)
            {
                if (Instances.ContainsKey(key))
                {
                    appItem = Instances [key];
                }
                else
                {
                    DesktopAppInfo item = null;
                    try {
                        item    = DesktopAppInfo.NewFromFilename(path);
                        appItem = new ApplicationItem(item);
                    } catch (Exception e) {
                        appItem = null;
                        try { item.Dispose(); } catch { }
                        Do.Platform.Log.Error("Could not load desktop item: {0}", e.Message);
                        Do.Platform.Log.Debug(e.StackTrace);
                    }

                    if (appItem != null)
                    {
                        Instances [key] = appItem;
                    }
                }
            }
            return(appItem);
        }
Example #4
0
        void OnFileCreated(object sender, FileSystemEventArgs e)
        {
            Log <ApplicationItemSource> .Debug("New Desktop file found: {0}", e.FullPath);

            var newItem = ApplicationItem.MaybeCreateFromDesktopItem(e.FullPath);

            if (newItem == null)
            {
                Log.Error("Found new Desktop file {0} but unable to create an item in the Universe", e.FullPath);
                return;
            }
            lock (app_items) {
                if (app_items.ContainsKey(e.FullPath))
                {
                    Log.Error("Attempting to add duplicate ApplicationItem {0} to Universe", e.FullPath);
                    return;
                }
                app_items[e.FullPath] = newItem;
            }
            RaiseItemsAvailable(new ItemsAvailableEventArgs()
            {
                newItems = new Item[] { newItem }
            });
        }
Example #5
0
        public static ApplicationItem MaybeCreateFromCmd(string cmd)
        {
            if (string.IsNullOrEmpty(cmd))
            {
                return(null);
            }

            List <ApplicationItem> appItems = new List <ApplicationItem> ();

            cmd = Regex.Escape(cmd);
            Regex regex = new Regex(string.Format("(^| ){0}( |)", cmd));

            foreach (ApplicationItem item in Instances.Values)
            {
                string path = item.Location;
                try {
                    if (path.StartsWith("file://"))
                    {
                        path = path.Substring("file://".Length);
                    }

                    path = Path.GetFileName(path);
                } catch { continue; }

                try {
                    if (!string.IsNullOrEmpty(path) && !string.IsNullOrEmpty(item.Exec) &&
                        (regex.IsMatch(path) || regex.IsMatch(item.Exec)))
                    {
                        appItems.Add(item);
                    }
                } catch {
                    // it failed, probably a null somewhere, we dont care really
                }
            }

            ApplicationItem bestMatch = null;

            foreach (ApplicationItem item in appItems)
            {
                if (bestMatch == null)
                {
                    bestMatch = item;
                    continue;
                }
                if (!item.Hidden)
                {
                    if (bestMatch.Hidden)
                    {
                        bestMatch = item;
                        continue;
                    }
                    if (item.ShouldShow)
                    {
                        if (!bestMatch.ShouldShow || item.Exec.Length < bestMatch.Exec.Length)
                        {
                            bestMatch = item;
                        }
                    }
                }
            }

            return(bestMatch);
        }
Example #6
0
 IEnumerable <CategoryItem> LoadCategoryItems(ApplicationItem appItem)
 {
     return(appItem.Categories
            .Where(c => !CategoryItem.ContainsCategory(c))
            .Select(CategoryItem.GetCategoryItem));
 }