1 7 package com.memoire.vainstall; 8 9 import java.io.BufferedWriter ; 10 import java.io.File ; 11 import java.io.FileNotFoundException ; 12 import java.io.FileOutputStream ; 13 import java.io.IOException ; 14 import java.io.OutputStreamWriter ; 15 import java.io.UnsupportedEncodingException ; 16 import java.util.List ; 17 import java.util.Set ; 18 19 23 public class VALinkLinux { 24 25 public static File getIconDir() { 26 return new File (System.getProperty("user.home"), "Desktop"); 27 } 28 29 public static void createAll(VAShortcutEntry[] _e, Set file) 30 throws IOException { 31 VALinkLinux link = new VALinkLinux(); 32 File dest = VAGlobals.SHORTCUTS_IN_INSTALLDIR ? new File ( 33 VAGlobals.DEST_PATH) : null; 34 File kdeMenuDir, gnomeMenuDir; 35 36 kdeMenuDir = link.createKDEMenuDir(file); 37 gnomeMenuDir = link.createGnomeMenuDir(file); 38 for (int i = 0; i < _e.length; i++) { 39 if (_e[i].isCreateOnDesktop()) { 40 File f = link.writeIconEntryOnDesktop(_e[i]); 41 if (f != null) 42 file.add(f.getAbsolutePath()); 43 } 44 if (VAGlobals.SHORTCUTS_IN_INSTALLDIR) { 45 File f = link.writeIconEntrynDir(_e[i], dest); 46 if (f != null) 47 file.add(f.getAbsolutePath()); 48 49 } 50 if (kdeMenuDir != null) { 51 File f = link.writeIconEntrynDir(_e[i], kdeMenuDir); 52 if (f != null) 53 file.add(f.getAbsolutePath()); 54 } 55 if (gnomeMenuDir != null) { 56 File f = link.writeIconEntrynDir(_e[i], gnomeMenuDir); 57 if (f != null) 58 file.add(f.getAbsolutePath()); 59 } 60 } 61 62 } 63 64 File desktopDir_; 65 66 List iconEntry_; 67 68 public VALinkLinux() { 69 70 } 71 72 public String getDesktopFileName(VAShortcutEntry _e) { 73 return _e.getName() + ".desktop"; 74 } 75 76 private File writeIcon(File _desktopDir, VAShortcutEntry _e) 77 throws IOException { 78 File dest = new File (_desktopDir, getDesktopFileName(_e)); 79 String encoding = "UTF-8"; 81 BufferedWriter w = null; 82 try { 86 w = new BufferedWriter (new OutputStreamWriter (new FileOutputStream ( 87 dest.getAbsolutePath()), encoding)); 88 w.write("[Desktop Entry]" + VAConstant.LINE_SEP); 89 w.write("Encoding=" + encoding + VAConstant.LINE_SEP); 90 if (_e.getType() != null) 91 w.write("Type=" + _e.getType() + VAConstant.LINE_SEP); 92 if (_e.getIconPath() != null) { 93 w.write("Icon=" + _e.getIconPath() + VAConstant.LINE_SEP); 94 } 95 if (_e.getWorkingDirectory() != null) { 96 w.write("Path=" + _e.getWorkingDirectory() 97 + VAConstant.LINE_SEP); 98 } 99 w.write("Name=" + _e.getName() + VAConstant.LINE_SEP); 100 w.write("Exec=" + _e.getExePath() + VAConstant.LINE_SEP); 101 if (_e.getComment() != null) { 102 w.write("Comment=" + _e.getComment() + VAConstant.LINE_SEP); 103 } 104 w.write("Terminal=" + (_e.isLaunchInTerminal() ? "true" : "false") 105 + VAConstant.LINE_SEP); 106 w.flush(); 107 } catch (UnsupportedEncodingException _e1) { 108 throw _e1; 109 } catch (FileNotFoundException _e1) { 110 throw _e1; 111 } catch (IOException _e1) { 112 throw _e1; 113 } finally { 114 if (w != null) 115 w.close(); 116 } 117 return dest; 118 } 119 120 124 public File writeIconEntryOnDesktop(VAShortcutEntry _e) throws IOException { 125 if (desktopDir_ == null) 126 desktopDir_ = getIconDir(); 127 if (_e.getIconPath() == null) { 128 if (_e.isUninstall()) { 129 _e.setIconPath(getIcon("uninstall", false)); 130 } else 131 _e.setIconPath(getIcon(_e.getName(), true)); 132 } 133 return writeIcon(desktopDir_, _e); 134 135 } 136 137 141 public File writeIconEntrynDir(VAShortcutEntry _e, File _destDir) 142 throws IOException { 143 if (_e.getIconPath() == null) { 144 if (_e.isUninstall()) { 145 _e.setIconPath(getIcon("uninstall", false)); 146 } else 147 _e.setIconPath(getIcon(_e.getName(), true)); 148 } 149 return writeIcon(_destDir, _e); 150 151 } 152 153 public String [] getIconFmt() { 154 return new String [] { "xpm", "png", "jpg" }; 155 } 156 157 public File findIcon(File _dir, String _name, String [] ext) { 158 for (int i = ext.length - 1; i >= 0; i--) { 159 File r = new File (_dir, _name + "." + ext[i]); 160 if (r.exists()) 161 return r; 162 } 163 return null; 164 } 165 166 public String getIcon(String _name, boolean useDefaultIcon) { 167 File common_icon = null; 168 File icons_dir = new File (VAGlobals.DEST_PATH); 169 String [] ext = getIconFmt(); 170 if ((VAGlobals.LINK_ENTRY_ICON != null) 171 && (VAGlobals.LINK_ENTRY_ICON.length() > 0)) { 172 File icon = new File (VAGlobals.DEST_PATH, VAGlobals.LINK_ENTRY_ICON 173 .replace('/', File.separatorChar)); 174 icons_dir = icon.getParentFile(); 175 common_icon = findIcon(icons_dir, icon.getName(), ext); 176 } 177 File script_icon = findIcon(icons_dir, _name, ext); 178 if (script_icon != null && script_icon.exists()) 179 return script_icon.getAbsolutePath(); 180 if (useDefaultIcon && common_icon != null && common_icon.exists()) 181 return common_icon.getAbsolutePath(); 182 return null; 183 } 184 185 public File createKDEMenuDir(Set shortcuts) throws IOException { 186 boolean r=false; 187 File tempFile, f; 188 String prefixDir; 189 190 192 prefixDir = System.getProperty("user.home")+ 193 File.separator + ".kde"+ 194 File.separator + "share"; 195 f = new File (prefixDir + File.separator + "applnk-mdk"); 196 if (!f.exists()) 197 f = new File (prefixDir + File.separator + "applnk"); 198 if (f.exists()) { 199 tempFile = new java.io.File (f, VAGlobals.LINK_SECTION_NAME); 200 if (!tempFile.exists()) { 201 if (tempFile.mkdirs()) { 202 shortcuts.add(tempFile.getAbsolutePath()); 203 f = tempFile; 204 } 205 } 206 else if(tempFile.isDirectory()) { 207 f = tempFile; 208 shortcuts.add(tempFile.getAbsolutePath()); 209 } 210 } 211 else { 212 f = null; 213 } 214 return f; 215 } 216 217 public File createGnomeMenuDir(Set shortcuts) throws IOException { 218 boolean r=false; 219 File tempFile, f; 220 String tempDir; 221 222 224 tempDir = System.getProperty("user.home")+ 225 File.separator + ".gnome"+ 226 File.separator + "apps"; 227 f = new File (tempDir); 228 if (f.exists()) { 229 tempFile = new java.io.File (f, VAGlobals.LINK_SECTION_NAME); 230 if (!tempFile.exists()) { 231 if (tempFile.mkdirs()) { 232 shortcuts.add(tempFile.getAbsolutePath()); 233 f = tempFile; 234 } 235 } 236 else if(tempFile.isDirectory()) { 237 f = tempFile; 238 shortcuts.add(tempFile.getAbsolutePath()); 239 } 240 } 241 else { 242 f = null; 243 } 244 return f; 245 } 246 247 } | Popular Tags |