예제 #1
0
        public static ImageSource Load(string path)
        {
            Logger.WoxDebug($"load begin {path}");
            var img = _cache.GetOrAdd(path, LoadInternal);

            Logger.WoxTrace($"load end {path}");
            return(img);
        }
예제 #2
0
        /// <summary>
        /// Searches the places.sqlite db and returns all bookmarks
        /// </summary>
        public List <Bookmark> GetBookmarks()
        {
            // Return empty list if the places.sqlite file cannot be found
            if (string.IsNullOrEmpty(PlacesPath) || !File.Exists(PlacesPath))
            {
                return(new List <Bookmark>());
            }
            Logger.WoxDebug($"Firefox db path {PlacesPath}");

            var bookmarList = new List <Bookmark>();

            // create the connection string and init the connection

            string dbPath = $"Data Source={PlacesPath}";

            using (var dbConnection = new SqliteConnection(dbPath))
            {
                // Open connection to the database file and execute the query
                dbConnection.Open();
                SqliteCommand command = dbConnection.CreateCommand();
                command.CommandText = @"SELECT moz_places.url, moz_bookmarks.title
                        FROM moz_places
                        INNER JOIN moz_bookmarks ON (
                            moz_bookmarks.fk NOT NULL AND moz_bookmarks.fk = moz_places.id
                        )";
                using (SqliteDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        string url   = reader.GetString(0) ?? string.Empty;
                        string title = reader.GetString(1) ?? string.Empty;
                        Logger.WoxTrace($"Firefox bookmark: <{title}> <{url}>");
                        bookmarList.Add(new Bookmark()
                        {
                            Name = title,
                            Url  = url,
                        });
                    }
                }
            }

            return(bookmarList);
        }
예제 #3
0
파일: ImageLoader.cs 프로젝트: vostory/Wox
        private static ImageSource LoadInternal(string path)
        {
            Logger.WoxDebug($"load from disk {path}");

            ImageSource image;

            if (string.IsNullOrEmpty(path))
            {
                image = GetErrorImage();
                return(image);
            }

            if (path.StartsWith("data:", StringComparison.OrdinalIgnoreCase))
            {
                var bitmapImage = new BitmapImage(new Uri(path))
                {
                    DecodePixelHeight = 32,
                    DecodePixelWidth  = 32
                };
                image = bitmapImage;
                return(image);
            }

            bool normalImage = ImageExtensions.Any(e => path.EndsWith(e));

            if (!Path.IsPathRooted(path) && normalImage)
            {
                path = Path.Combine(Constant.ProgramDirectory, "Images", Path.GetFileName(path));
            }


            var parent1 = new DirectoryInfo(Constant.ProgramDirectory);
            var parent2 = new DirectoryInfo(DataLocation.DataDirectory());
            var subPath = new DirectoryInfo(path);

            Logger.WoxTrace($"{path} {subPath} {parent1} {parent2}");
            bool imageInsideWoxDirectory = IsSubdirectory(parent1, subPath) || IsSubdirectory(parent2, subPath);

            if (normalImage && imageInsideWoxDirectory)
            {
                image = new BitmapImage(new Uri(path))
                {
                    DecodePixelHeight = 32,
                    DecodePixelWidth  = 32
                };
                image.Freeze();
                return(image);
            }

            if (Directory.Exists(path))
            {
                // can be extended to support guid things
                ShellObject shell = ShellFile.FromParsingName(path);
                image = shell.Thumbnail.SmallBitmapSource;
                image.Freeze();
                return(image);
            }

            if (File.Exists(path))
            {
                try
                {
                    // https://stackoverflow.com/a/1751610/2833083
                    // https://stackoverflow.com/questions/21751747/extract-thumbnail-for-any-file-in-windows
                    // https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ishellitemimagefactory-getimage
                    ShellFile shell = ShellFile.FromFilePath(path);
                    // https://github.com/aybe/Windows-API-Code-Pack-1.1/blob/master/source/WindowsAPICodePack/Shell/Common/ShellThumbnail.cs#L333
                    // https://github.com/aybe/Windows-API-Code-Pack-1.1/blob/master/source/WindowsAPICodePack/Shell/Common/DefaultShellImageSizes.cs#L46
                    // small is (32, 32)
                    image = shell.Thumbnail.SmallBitmapSource;
                    image.Freeze();
                    return(image);
                }
                catch (ShellException e1)
                {
                    try
                    {
                        // sometimes first try will throw exception, but second try will be ok.
                        // so we try twice
                        // Error while extracting thumbnail for C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Steam\\Steam.lnk
                        ShellFile shellFile = ShellFile.FromFilePath(path);
                        image = shellFile.Thumbnail.SmallBitmapSource;
                        image.Freeze();
                        return(image);
                    }
                    catch (System.Exception e2)
                    {
                        Logger.WoxError($"Failed to get thumbnail, first, {path}", e1);
                        Logger.WoxError($"Failed to get thumbnail, second, {path}", e2);
                        image = GetErrorImage();
                        return(image);
                    }
                }
            }
            else
            {
                image = GetErrorImage();
                return(image);
            }
        }
예제 #4
0
        public static ImageSource GetImage(string key, string path, int iconSize)
        {
            // https://github.com/CoenraadS/Windows-Control-Panel-Items/
            // https://gist.github.com/jnm2/79ed8330ceb30dea44793e3aa6c03f5b

            string iconStringRaw = path.Substring(key.Length);
            var    iconString    = new List <string>(iconStringRaw.Split(new[] { ',' }, 2));
            IntPtr iconPtr       = IntPtr.Zero;
            IntPtr dataFilePointer;
            IntPtr iconIndex;
            uint   LOAD_LIBRARY_AS_DATAFILE = 0x00000002;

            Logger.WoxTrace($"{nameof(iconStringRaw)}: {iconStringRaw}");

            if (string.IsNullOrEmpty(iconString[0]))
            {
                var e = new ArgumentException($"iconString empth {path}");
                e.Data.Add(nameof(path), path);
                throw e;
            }

            if (iconString[0][0] == '@')
            {
                iconString[0] = iconString[0].Substring(1);
            }

            dataFilePointer = LoadLibraryEx(iconString[0], IntPtr.Zero, LOAD_LIBRARY_AS_DATAFILE);
            if (iconString.Count == 2)
            {
                // C:\WINDOWS\system32\mblctr.exe,0
                // %SystemRoot%\System32\FirewallControlPanel.dll,-1
                var index = Math.Abs(int.Parse(iconString[1]));
                iconIndex = (IntPtr)index;
                iconPtr   = LoadImage(dataFilePointer, iconIndex, 1, iconSize, iconSize, 0);
            }

            if (iconPtr == IntPtr.Zero)
            {
                IntPtr defaultIconPtr = IntPtr.Zero;
                var    callback       = new EnumResNameDelegate((hModule, lpszType, lpszName, lParam) =>
                {
                    defaultIconPtr = lpszName;
                    return(false);
                });
                var result = EnumResourceNamesWithID(dataFilePointer, GROUP_ICON, callback, IntPtr.Zero); //Iterate through resources.
                if (!result)
                {
                    int error = Marshal.GetLastWin32Error();
                    int userStoppedResourceEnumeration = 0x3B02;
                    if (error != userStoppedResourceEnumeration)
                    {
                        Win32Exception exception = new Win32Exception(error);
                        exception.Data.Add(nameof(path), path);
                        throw exception;
                    }
                }
                iconPtr = LoadImage(dataFilePointer, defaultIconPtr, 1, iconSize, iconSize, 0);
            }

            FreeLibrary(dataFilePointer);
            BitmapSource image;

            if (iconPtr != IntPtr.Zero)
            {
                image = Imaging.CreateBitmapSourceFromHIcon(iconPtr, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                image.CloneCurrentValue(); //Remove pointer dependancy.
                image.Freeze();
                DestroyIcon(iconPtr);
                return(image);
            }
            else
            {
                var e = new ArgumentException($"iconPtr zero {path}");
                e.Data.Add(nameof(path), path);
                throw e;
            }
        }