summaryrefslogtreecommitdiff
path: root/SparkleShare/Windows/UserInterface/Shortcut.cs
blob: aded16ef1209b1eeef422b95af467f830c79f311 (plain)
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
//   SparkleShare, a collaboration and sharing tool.
//   Copyright (C) 2010  Hylke Bons <hi@planetpeanut.uk>
//
//   This program is free software: you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
//
//   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, see <http://www.gnu.org/licenses/>.


using System;
using System.Runtime.InteropServices;
using System.Text;

namespace SparkleShare {

   public class Shortcut : IDisposable {
        
        private IShellLinkW link;
        
        public Shortcut ()
        {
        }
        
        
        public void Create (string file_path, string target_path)
        {
            link = (IShellLinkW) new CShellLink ();
            this.link.SetShowCmd (1);        
            this.link.SetPath (target_path);
            (this.link as IPersistFile).Save (file_path, true);
        }
            
        
        public void Dispose () {
            if (this.link == null )
                return;
            
            Marshal.ReleaseComObject (this.link);
            this.link = null;
        }
        
        
        ~Shortcut ()
        {
            Dispose ();
        }
        

        private class UnManagedMethods {
            [DllImport ("Shell32", CharSet = CharSet.Auto)]
            internal extern static int ExtractIconEx (
                [MarshalAs(UnmanagedType.LPTStr)] string lpszFile, int nIconIndex,
                IntPtr[] phIconLarge, IntPtr[] phIconSmall,    int nIcons);
        
            [DllImport ("user32")]
            internal static extern int DestroyIcon (IntPtr hIcon);
        }
        
        
        [StructLayoutAttribute (LayoutKind.Sequential, Pack = 4, Size = 0)]
        private struct _FILETIME {
            public uint dwLowDateTime;
            public uint dwHighDateTime;
        }  
        
        
        [StructLayoutAttribute(LayoutKind.Sequential, Pack = 4, Size = 0, CharSet = CharSet.Unicode)]
        private struct _WIN32_FIND_DATAW {
            public uint dwFileAttributes;
            public _FILETIME ftCreationTime;
            public _FILETIME ftLastAccessTime;
            public _FILETIME ftLastWriteTime;
            public uint nFileSizeHigh;
            public uint nFileSizeLow;
            public uint dwReserved0;
            public uint dwReserved1;
            
            [MarshalAs(UnmanagedType.ByValTStr , SizeConst = 260)]
            public string cFileName;
            
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
            public string cAlternateFileName;
        }
        
        
        [ComImportAttribute ()]
        [GuidAttribute ("0000010C-0000-0000-C000-000000000046")]
        [InterfaceTypeAttribute (ComInterfaceType.InterfaceIsIUnknown)]
        private interface IPersist {
            [PreserveSig]
            void GetClassID (out Guid pClassID);
        }
        
        
        [ComImportAttribute ()]
        [GuidAttribute ("0000010B-0000-0000-C000-000000000046")]
        [InterfaceTypeAttribute (ComInterfaceType.InterfaceIsIUnknown)]
        private interface IPersistFile {
            [PreserveSig]
            void GetClassID (out Guid pClassID);
            void IsDirty ();
            
            void Load(
                [MarshalAs (UnmanagedType.LPWStr)] string pszFileName, 
                uint dwMode);
            
            void Save(
                [MarshalAs (UnmanagedType.LPWStr)] string pszFileName, 
                [MarshalAs (UnmanagedType.Bool)] bool fRemember);
            
            void SaveCompleted(
                [MarshalAs (UnmanagedType.LPWStr)] string pszFileName);
            
            void GetCurFile (
                [MarshalAs (UnmanagedType.LPWStr)] out string ppszFileName);
        }
        
        
        [ComImportAttribute()]
        [GuidAttribute("000214F9-0000-0000-C000-000000000046")]
        [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
        private interface IShellLinkW
        {
            void GetPath(
                [Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, 
                int cchMaxPath, 
                ref _WIN32_FIND_DATAW pfd, 
                uint fFlags);
            
            void GetIDList(out IntPtr ppidl);
            void SetIDList(IntPtr pidl);
            
            void GetDescription(
                [Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile,
                int cchMaxName);
            
            void SetDescription(
                [MarshalAs(UnmanagedType.LPWStr)] string pszName);
                
            void GetWorkingDirectory(
                [Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir,
                int cchMaxPath);
            
            void SetWorkingDirectory(
                [MarshalAs(UnmanagedType.LPWStr)] string pszDir);
                
            void GetArguments(
                [Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs, 
                int cchMaxPath);
            
            void SetArguments(
                [MarshalAs(UnmanagedType.LPWStr)] string pszArgs);
                
            void GetHotkey(out short pwHotkey);
            void SetHotkey(short pwHotkey);
            
            void GetShowCmd(out uint piShowCmd);
            void SetShowCmd(uint piShowCmd);
            
            void GetIconLocation(
                [Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath, 
                int cchIconPath, 
                out int piIcon);
            
            void SetIconLocation(
                [MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, 
                int iIcon);
            
            void SetRelativePath(
                [MarshalAs(UnmanagedType.LPWStr)] string pszPathRel, 
                uint dwReserved);
            
            void Resolve(IntPtr hWnd, uint fFlags);
            
            void SetPath(
                [MarshalAs(UnmanagedType.LPWStr)] string pszFile);
        }
    
        
        [GuidAttribute ("00021401-0000-0000-C000-000000000046")]
        [ClassInterfaceAttribute (ClassInterfaceType.None)]
        [ComImportAttribute ()]
        private class CShellLink {}
    }
}