1 /* 2 * Copyright 2008, orangevolt.sf.net, and individual contributors as indicated 3 * by the @authors tag. See the copyright.txt in the distribution for a 4 * full listing of individual contributors. 5 * 6 * This is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU Lesser General Public License as 8 * published by the Free Software Foundation; either version 3 of 9 * the License, or (at your option) any later version. 10 * 11 * This software is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this software; if not, write to the Free 18 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 19 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 20 * 21 * $Id: LnkFile.java 125 2008-11-04 07:18:28Z arnep $ 22 * 23 */ 24 package net.sf.ovanttasks.ovnative.win32; 25 26 import java.io.File; 27 28 /** 29 * LnkFile reads and writes Windows Shortcut files in the .lnk format. 30 * It can be used to create or read <i>.lnk</i> files which are used 31 * by Microsoft for Shortcuts (the IShellLink Interface). 32 * The destination (path) can be executable (cmd.exe) or normal files (text files and more). 33 * They will be opend / executed by the shell. 34 * <p> 35 * The HOTKEYF_XXX constants are taken from (MinGW) commctrl.h. 36 * <p> 37 * The SW_SHOWXX constants are taken from (MinGW) winuser.h. 38 * 39 * @author lars.gersmann@roxes.com 40 * @author arnep@users.sf.net 41 */ 42 public class LnkFile { 43 44 private static final int SW_SHOWNORMAL = 1; 45 private static final int SW_SHOWMINIMIZED = 2; 46 private static final int SW_SHOWMAXIMIZED = 3; 47 48 /** 49 * Returns the initil state in wich the window will be appear on screen. 50 * This can be normal, minimalized or maxiamlized. 51 * @return The sow state 52 */ 53 public ShowCmd getShowState() { 54 return ShowCmd.valueOf(showCmd); 55 } 56 57 /** 58 * Sets the initial state in wich the window will be appear on screen. 59 * This can be normal, minimalized or maxiamlized. 60 * @param swShow The state to set. 61 */ 62 public void setShowState(ShowCmd swShow) { 63 this.showCmd = swShow.swShow; 64 } 65 66 /** 67 * The inital state of the window. 68 */ 69 public static enum ShowCmd { 70 71 /** 72 * normal window state 73 */ 74 SHOWNORMAL(SW_SHOWNORMAL), 75 /** 76 * maximized window state 77 */ 78 SHOWMAXIMIZED(SW_SHOWMAXIMIZED), 79 /** 80 * minimized window state 81 */ 82 SHOWMINIMIZED(SW_SHOWMINIMIZED); 83 84 final int swShow; 85 86 private static ShowCmd valueOf(int swShow) { 87 for (ShowCmd cmd : values()) { 88 if (cmd.swShow == swShow) { 89 return cmd; 90 } 91 } 92 return null; 93 } 94 95 private ShowCmd(int swShow) { 96 this.swShow = swShow; 97 } 98 } 99 private String arguments; 100 private String toolTipText; 101 private File srcFile; 102 private int iconIndex = -1; 103 private String iconLocation; 104 private String targetFile; 105 private String workingDirectory; 106 private short hotkey; 107 private int showCmd = SW_SHOWNORMAL; 108 109 /** 110 * Create a new <code>LnkFile</code> instance for a specified location. 111 * <p> 112 * {@link File#File(java.lang.String, java.lang.String)} 113 * If <code>load</code> is <code>true</code> the instance of 114 * <code>LnkFile</code> is initialized with the values from the file. 115 * <p> 116 * The native library is also loaded. 117 * 118 * @param directory The parent pathname string 119 * @param name The child pathname 120 * @param load wether or not load the data from existing file. 121 */ 122 public LnkFile(final String directory, final String name, final boolean load) { 123 OvNativeLibLoader.loadLib(); 124 setSrcFile(directory, name); 125 if (load) { 126 load(); 127 } 128 } 129 130 /** 131 * Create a new <code>LnkFile</code> instance for a specified location. 132 * <p> 133 * If <code>load</code> is <code>true</code> the instance of 134 * <code>LnkFile</code> is initialized with the values from the <code>lnkFile</code>. 135 * <p> 136 * The native library is also loaded. 137 * 138 * @param lnkFile The lnkFile to use 139 * @param load wether or not load the data from existing file. 140 */ 141 public LnkFile(final File lnkFile, final boolean load) { 142 OvNativeLibLoader.loadLib(); 143 if (!lnkFile.getName().endsWith(".lnk")) { 144 throw new IllegalArgumentException("Link file name must end with \".lnk\""); 145 } 146 this.srcFile = lnkFile; 147 if (load) { 148 load(); 149 } 150 } 151 152 /** 153 * Returns the arguments of the shortcut. 154 * The path points to an executable (<code>path</code>), the arguments 155 * specified here will be passed to the executable. 156 * 157 * @return The arguments of the shortcut 158 */ 159 public String getArguments() { 160 return arguments; 161 } 162 163 /** 164 * Returns the toolTipText for this shortcut. 165 * The toolTipText is visibe to the user if the mouse 166 * hoovers longer over the file. 167 * 168 * @return the description of this shortcut 169 */ 170 public String getToolTipText() { 171 return toolTipText; 172 } 173 174 /** 175 * Returns the source of the Link. 176 * This is the file with the .lnk extentiuon. 177 * @return 178 */ 179 public File getSrcFile() { 180 return srcFile; 181 } 182 183 /** 184 * Returns the icon index for this shortcut. 185 * This is the index within the .ico file or the index in 186 * the icon resources of a .dll or .exe 187 * 188 * @return the index of the icon within the specified location. 189 */ 190 public int getIconIndex() { 191 return iconIndex; 192 } 193 194 /** 195 * Returns the icon location for this shortcut. 196 * This can be a .ico, .dll or .exe file. The <code>iconIndex</code> 197 * indicates the index of the icon within the file. 198 * @return the location of the icon for the shortcut. 199 */ 200 public String getIconLocation() { 201 return iconLocation; 202 } 203 204 /** 205 * Returns the taget for this shortcut. 206 * The target is an executabel file , or can be an ordinary 207 * file for example a .txt file. 208 * @return The taget of this shortcut as absolute path. 209 */ 210 public String getTargetFile() { 211 return targetFile; 212 } 213 214 /** 215 * Returns the working directory for this shortcut. 216 * 217 * @return The working directory for the shortcut. 218 */ 219 public String getWorkingDirectory() { 220 return workingDirectory; 221 } 222 223 /** 224 * Loads the link data from the filesystem. 225 * @throws net.sf.ovanttasks.ovnative.win32.Win32Exception 226 */ 227 public void load() throws Win32Exception { 228 load(srcFile.getAbsolutePath()); 229 } 230 231 private native void load(String lnkFileName) throws Win32Exception; 232 233 /** 234 * Resolve the link with UI interaction 235 * @throws net.sf.ovanttasks.ovnative.win32.Win32Exception 236 */ 237 public void resolve() throws Win32Exception { 238 resolve(srcFile.getAbsolutePath()); 239 } 240 241 private native void resolve(String filename) throws Win32Exception; 242 243 /** 244 * Save the link 245 * @param directory 246 * @param name 247 * @throws net.sf.ovanttasks.ovnative.win32.Win32Exception 248 */ 249 public void save(final String directory, final String name) throws Win32Exception { 250 setSrcFile(directory, name); 251 save(); 252 } 253 254 /** 255 * Save the link 256 * @throws net.sf.ovanttasks.ovnative.win32.Win32Exception 257 */ 258 public void save() throws Win32Exception { 259 save(srcFile.getAbsolutePath()); 260 } 261 262 private native void save(String filename) throws Win32Exception; 263 264 /** 265 * Set the Arguments 266 * @param arguments the arguments to set. 267 */ 268 public void setArguments(final String arguments) { 269 this.arguments = arguments; 270 } 271 272 /** 273 * Set the toolTipText for this shortcut. 274 * The toolTipText is visible toi the user, 275 * if the mouse hoovers over the file. 276 * 277 * @param toolTipText The tooltiptext to display 278 */ 279 public void setToolTipText(final String toolTipText) { 280 this.toolTipText = toolTipText; 281 } 282 283 /** 284 * Set the icon location for this shortcut. 285 * The icon will be displayed on the screen. 286 * The file can be an ico,dll or exe the <code>iconindex</code> 287 * is the index in the file. 288 * 289 * Both the file and the index into the file must be specified. 290 * @param iconLocation The path to the file. 291 * @param iconIndex The index of the icon within the file. 292 */ 293 public void setIcon(final String iconLocation, final int iconIndex) { 294 this.iconLocation = iconLocation; 295 this.iconIndex = iconIndex; 296 } 297 298 /** 299 * Set the target for this shortcut. 300 * 301 * The target ist the path to the file of this link. If this is an executable, 302 * additional <code>arguments</code> and a <code>workingDirectory</code> 303 * can be provided. 304 * 305 * @param targetFile The target of this link 306 */ 307 public void setTargetFile(final String targetFile) { 308 this.targetFile = targetFile; 309 } 310 311 /** 312 * Set the working directory for this shortcut. 313 * The <code>workingDirectory</code> is the directory in which the executable 314 * given by <code>path</code> will be executed. 315 * 316 * 317 * @param workingDirectory The working directory 318 */ 319 public void setWorkingDirectory(final String workingDirectory) { 320 this.workingDirectory = workingDirectory; 321 } 322 323 /** 324 * Sets the hotkey. 325 * 326 * The hotkey is used to execute the link given by <code>path>/code> with keystoken. 327 * The following modifiers will be supported: 328 * <li> 329 * <item>Alt the Alt key.</item> 330 * <item>Ctrl the Ctrl key,</item> 331 * <item>Shift the Shift key</item> 332 * </li> 333 * Each separated by " + ". 334 * Example: <i>Ctrl + Shift + Alt + A</i>. 335 * @param hotkey The hotkey as String. 336 */ 337 public void setHotkey(final String hotkey) { 338 this.hotkey = HotKeyTools.parseString(hotkey); 339 } 340 341 /** 342 * Returns the hotkey. 343 * The Hotkey will be returned as String {@link setHotKey}. 344 * @return the hotkey as String 345 */ 346 public String getHotkey() { 347 return HotKeyTools.toString(hotkey); 348 } 349 350 /** 351 * The <code>file</code> in the filesystem which is the source of this link. 352 * 353 * The srcFile holds the real name of the shelllink. 354 * If the file extention ".lnk" is not given, it will be appended. 355 * The extention (.lnk)is usually not visible on Windows. 356 * @param directory The name of the directory. 357 * @param name The filenae in the directory 358 */ 359 private void setSrcFile(final String directory, String name) { 360 if (!name.endsWith(".lnk")) { 361 name += ".lnk"; 362 } 363 srcFile = new File(directory, name); 364 } 365 }