private void RegisterTrayWnd()
        {
            ushort trayClassReg = RegisterWndClass(TrayWndClass);

            if (trayClassReg == 0)
            {
                ShellLogger.Info($"TrayService: Error registering {TrayWndClass} class ({Marshal.GetLastWin32Error()})");
            }

            HwndTray = CreateWindowEx(
                ExtendedWindowStyles.WS_EX_TOPMOST |
                ExtendedWindowStyles.WS_EX_TOOLWINDOW, trayClassReg, "",
                WindowStyles.WS_POPUP | WindowStyles.WS_CLIPCHILDREN |
                WindowStyles.WS_CLIPSIBLINGS, 0, 0, GetSystemMetrics(0),
                (int)(23 * DpiHelper.DpiScale), IntPtr.Zero, IntPtr.Zero, hInstance, IntPtr.Zero);

            if (HwndTray == IntPtr.Zero)
            {
                ShellLogger.Info($"TrayService: Error creating {TrayWndClass} window ({Marshal.GetLastWin32Error()})");
            }
            else
            {
                ShellLogger.Debug($"TrayService: Created {TrayWndClass}");
            }
        }
        private void ChangedEventHandler(object sender, FileSystemEventArgs e)
        {
            Task.Factory.StartNew(() =>
            {
                ShellLogger.Info($"ShellFolder: Item {e.ChangeType}: {e.Name} ({e.FullPath})");

                bool exists = false;

                foreach (var file in Files)
                {
                    if (_isDisposed)
                    {
                        break;
                    }

                    if (file.Path == e.FullPath)
                    {
                        exists = true;
                        file.Refresh();

                        break;
                    }
                }

                if (!exists)
                {
                    AddFile(e.FullPath);
                }
            }, CancellationToken.None, TaskCreationOptions.None, Interop.ShellItemScheduler);
        }
Exemple #3
0
        private async Task <bool> CheckForUpdate()
        {
            try
            {
                VersionInfo versionInfo = await httpClient.GetFromJsonAsync <VersionInfo>(_versionUrl);

                if (Version.TryParse(versionInfo.Version, out Version newVersion))
                {
                    if (newVersion > _currentVersion)
                    {
                        return(true);
                    }
                }
                else
                {
                    ShellLogger.Info($"Updater: Unable to parse new version");
                }
            }
            catch (Exception e)
            {
                ShellLogger.Info($"Updater: Unable to check for updates: {e.Message}");
            }

            return(false);
        }
        internal void Initialize()
        {
            if (IsInitialized)
            {
                return;
            }

            try
            {
                ShellLogger.Debug("TasksService: Starting");

                // create window to receive task events
                _HookWin = new NativeWindowEx();
                _HookWin.CreateHandle(new CreateParams());

                // prevent other shells from working properly
                SetTaskmanWindow(_HookWin.Handle);

                // register to receive task events
                RegisterShellHookWindow(_HookWin.Handle);
                WM_SHELLHOOKMESSAGE         = RegisterWindowMessage("SHELLHOOK");
                WM_TASKBARCREATEDMESSAGE    = RegisterWindowMessage("TaskbarCreated");
                TASKBARBUTTONCREATEDMESSAGE = RegisterWindowMessage("TaskbarButtonCreated");
                _HookWin.MessageReceived   += ShellWinProc;

                if (EnvironmentHelper.IsWindows8OrBetter)
                {
                    // set event hook for uncloak events
                    uncloakEventProc = UncloakEventCallback;

                    if (uncloakEventHook == IntPtr.Zero)
                    {
                        uncloakEventHook = SetWinEventHook(
                            EVENT_OBJECT_UNCLOAKED,
                            EVENT_OBJECT_UNCLOAKED,
                            IntPtr.Zero,
                            uncloakEventProc,
                            0,
                            0,
                            WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS);
                    }
                }

                // set window for ITaskbarList
                setTaskbarListHwnd(_HookWin.Handle);

                // adjust minimize animation
                SetMinimizedMetrics();

                // enumerate windows already opened and set active window
                getInitialWindows();

                IsInitialized = true;
            }
            catch (Exception ex)
            {
                ShellLogger.Info("TasksService: Unable to start: " + ex.Message);
            }
        }
        private void DeletedEventHandler(object sender, FileSystemEventArgs e)
        {
            Task.Factory.StartNew(() =>
            {
                ShellLogger.Info($"ShellFolder: Item {e.ChangeType}: {e.Name} ({e.FullPath})");

                RemoveFile(e.FullPath);
            }, CancellationToken.None, TaskCreationOptions.None, Interop.ShellItemScheduler);
        }
        private void folderAction(uint action, string path)
        {
            ShellLogger.Info($"Command: {action} Path: {path}");

            if (action == (uint)CommonContextMenuItem.Properties)
            {
                ShellHelper.ShowFileProperties(path);
            }
            else if (action == (uint)CommonContextMenuItem.Paste)
            {
                _fileWorker.PasteFromClipboard(path);
            }
        }
        private void RenamedEventHandler(object sender, RenamedEventArgs e)
        {
            Task.Factory.StartNew(() =>
            {
                ShellLogger.Info($"ShellFolder: Item {e.ChangeType}: From {e.OldName} ({e.OldFullPath}) to {e.Name} ({e.FullPath})");

                int existing = RemoveFile(e.OldFullPath);

                if (!FileExists(e.FullPath))
                {
                    AddFile(e.FullPath, existing);
                }
            }, CancellationToken.None, TaskCreationOptions.None, Interop.ShellItemScheduler);
        }
        private bool executeAction(string action, ShellItem[] items, bool allFolders)
        {
            bool handled = false;

            foreach (var item in items)
            {
                ShellLogger.Info($"Command: {action} Path: {item.Path}");

                if (action == "open" && allFolders)
                {
                    Navigate(item.Path);
                    handled = true;
                }
                else if (action == ((uint)CommonContextMenuItem.Properties).ToString())
                {
                    ShellHelper.ShowFileProperties(item.Path);
                    handled = true;
                }
            }

            return(handled);
        }
        private void RunStartupApps()
        {
            foreach (StartupEntry app in GetStartupApps())
            {
                string[] procInfo = expandArgs(app);

                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.UseShellExecute = true;
                startInfo.FileName        = procInfo[0];
                startInfo.Arguments       = procInfo[1];

                ShellLogger.Debug($"StartupRunner: Starting program: {startInfo.FileName}");

                try
                {
                    Process.Start(startInfo);
                }
                catch
                {
                    ShellLogger.Info($"StartupRunner: Failed to start program: {startInfo.FileName}");
                }
            }
        }
        private void RegisterNotifyWnd()
        {
            ushort trayNotifyClassReg = RegisterWndClass(NotifyWndClass);

            if (trayNotifyClassReg == 0)
            {
                ShellLogger.Info($"TrayService: Error registering {NotifyWndClass} class ({Marshal.GetLastWin32Error()})");
            }

            HwndNotify = CreateWindowEx(0, trayNotifyClassReg, null,
                                        WindowStyles.WS_CHILD | WindowStyles.WS_CLIPCHILDREN |
                                        WindowStyles.WS_CLIPSIBLINGS, 0, 0, GetSystemMetrics(0),
                                        (int)(23 * DpiHelper.DpiScale), HwndTray, IntPtr.Zero, hInstance, IntPtr.Zero);

            if (HwndNotify == IntPtr.Zero)
            {
                ShellLogger.Info($"TrayService: Error creating {NotifyWndClass} window ({Marshal.GetLastWin32Error()})");
            }
            else
            {
                ShellLogger.Debug($"TrayService: Created {NotifyWndClass}");
            }
        }
        private IntPtr WndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam)
        {
            switch ((WM)msg)
            {
            case WM.COPYDATA:
                if (lParam == IntPtr.Zero)
                {
                    ShellLogger.Debug("TrayService: CopyData is null");
                    break;
                }

                COPYDATASTRUCT copyData =
                    (COPYDATASTRUCT)Marshal.PtrToStructure(lParam, typeof(COPYDATASTRUCT));

                switch ((int)copyData.dwData)
                {
                case 0:
                    // AppBar message
                    if (Marshal.SizeOf(typeof(APPBARMSGDATAV3)) == copyData.cbData)
                    {
                        APPBARMSGDATAV3 amd = (APPBARMSGDATAV3)Marshal.PtrToStructure(copyData.lpData,
                                                                                      typeof(APPBARMSGDATAV3));

                        if (Marshal.SizeOf(typeof(APPBARDATAV2)) != amd.abd.cbSize)
                        {
                            ShellLogger.Debug("TrayService: Size incorrect for AppBarData");
                            break;
                        }

                        bool   handled   = false;
                        IntPtr abmResult = IntPtr.Zero;
                        if (appBarMessageDelegate != null)
                        {
                            abmResult = appBarMessageDelegate(amd, ref handled);
                        }

                        if (handled)
                        {
                            ShellLogger.Debug($"TrayService: Handled AppBar message {(ABMsg)amd.dwMessage}");
                            return(abmResult);
                        }

                        ShellLogger.Debug($"TrayService: Forwarding AppBar message {(ABMsg)amd.dwMessage}");
                    }
                    else
                    {
                        ShellLogger.Debug("TrayService: AppBar message received, but with unknown size");
                    }
                    break;

                case 1:
                    SHELLTRAYDATA trayData =
                        (SHELLTRAYDATA)Marshal.PtrToStructure(copyData.lpData,
                                                              typeof(SHELLTRAYDATA));
                    if (trayDelegate != null)
                    {
                        if (trayDelegate(trayData.dwMessage, new SafeNotifyIconData(trayData.nid)))
                        {
                            return((IntPtr)1);
                        }

                        ShellLogger.Debug("TrayService: Ignored notify icon message");
                    }
                    else
                    {
                        ShellLogger.Info("TrayService: TrayDelegate is null");
                    }
                    break;

                case 3:
                    WINNOTIFYICONIDENTIFIER iconData =
                        (WINNOTIFYICONIDENTIFIER)Marshal.PtrToStructure(copyData.lpData,
                                                                        typeof(WINNOTIFYICONIDENTIFIER));

                    if (iconDataDelegate != null)
                    {
                        return(iconDataDelegate(iconData.dwMessage, iconData.hWnd, iconData.uID,
                                                iconData.guidItem));
                    }

                    ShellLogger.Info("TrayService: IconDataDelegate is null");
                    break;
                }

                break;

            case WM.WINDOWPOSCHANGED:
                WINDOWPOS wndPos = WINDOWPOS.FromMessage(lParam);

                if ((wndPos.flags & SetWindowPosFlags.SWP_SHOWWINDOW) != 0)
                {
                    SetWindowLong(HwndTray, GWL_STYLE,
                                  GetWindowLong(HwndTray, GWL_STYLE) &
                                  ~(int)WindowStyles.WS_VISIBLE);

                    ShellLogger.Debug($"TrayService: {TrayWndClass} became visible; hiding");
                }
                break;
            }

            if (msg == (int)WM.COPYDATA ||
                msg == (int)WM.ACTIVATEAPP ||
                msg == (int)WM.COMMAND ||
                msg >= (int)WM.USER)
            {
                return(ForwardMsg(hWnd, msg, wParam, lParam));
            }

            return(DefWindowProc(hWnd, msg, wParam, lParam));
        }