1 20 21 package org.jdesktop.jdic.filetypes.internal; 22 23 import java.io.BufferedReader ; 24 import java.io.BufferedWriter ; 25 import java.io.File ; 26 import java.io.FileReader ; 27 import java.io.FileWriter ; 28 import java.io.IOException ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import org.jdesktop.jdic.filetypes.Action; 32 import org.jdesktop.jdic.filetypes.Association; 33 import org.jdesktop.jdic.filetypes.RegisterFailedException; 34 35 36 39 public class GnomeAppAssociationWriter implements AppAssociationWriter { 40 static String GNOMEDIR_VALUE = GnomeAssociationUtil.getEnv("GNOMEDIR"); 44 static String GNOME_LINUX_SYSTEM_SHARE_DIR = 45 (GNOMEDIR_VALUE == null) ? "/usr/share/" : (GNOMEDIR_VALUE + "/share/"); 46 static String GNOME_SOLARIS_SYSTEM_SHARE_DIR = "/usr/share/gnome/"; 47 48 static String GNOME_LINUX_SYSTEM_MIME_INFO_DIR = GNOME_LINUX_SYSTEM_SHARE_DIR + "mime-info/"; 50 static String GNOME_SOLARIS_SYSTEM_MIME_INFO_DIR = GNOME_SOLARIS_SYSTEM_SHARE_DIR + "mime-info/"; 51 52 static String GNOME_LINUX_SYSTEM_APPLICATION_REGISTRY_DIR = GNOME_LINUX_SYSTEM_SHARE_DIR 54 + "application-registry/"; 55 static String GNOME_SOLARIS_SYSTEM_APPLICATION_REGISTRY_DIR = GNOME_SOLARIS_SYSTEM_SHARE_DIR 56 + "application-registry/"; 57 58 static String OSNAME = System.getProperty("os.name").toLowerCase(); 60 static String GNOME_SYSTEM_MIME_INFO_DIR = 61 OSNAME.equals("linux") ? GNOME_LINUX_SYSTEM_MIME_INFO_DIR : GNOME_SOLARIS_SYSTEM_MIME_INFO_DIR; 62 static String GNOME_SYSTEM_APPLICATION_REGISTRY_DIR = 63 OSNAME.equals("linux") ? 64 GNOME_LINUX_SYSTEM_APPLICATION_REGISTRY_DIR : GNOME_SOLARIS_SYSTEM_APPLICATION_REGISTRY_DIR; 65 66 static String GNOME_USER_MIME_INFO_DIR = System.getProperty("user.home") + "/.gnome/mime-info/"; 68 static String GNOME_USER_APPLICATION_INFO_DIR = System.getProperty("user.home") + "/.gnome/application-info/"; 69 70 static String MIME_SUFFIX = ".mime"; 72 static String KEYS_SUFFIX = ".keys"; 73 static String APPLICATIONS_SUFFIX = ".applications"; 74 75 private String defaultAppID = null; 77 private String defaultAppCommand = null; 78 79 87 private String fileExtListToString(List fileExtList) { 88 String fileExtListString = ""; 89 Iterator fileExtIter = fileExtList.iterator(); 90 String temFileExt; 91 92 if (fileExtIter != null) { 93 while (fileExtIter.hasNext()) { 94 temFileExt = (String ) fileExtIter.next(); 95 96 if (temFileExt != null) { 97 temFileExt = AppUtility.removeDotFromFileExtension(temFileExt); 99 100 if (fileExtListString.length() == 0) { 101 fileExtListString = fileExtListString.concat(temFileExt); 102 } else { 103 fileExtListString = fileExtListString.concat(' ' + temFileExt); 104 } 105 } 106 } 107 } else { 108 fileExtListString = null; 109 } 110 111 return fileExtListString; 112 } 113 114 117 private String getSystemDotMimeFilePath(Association assoc) { 118 return GNOME_SYSTEM_MIME_INFO_DIR + assoc.getName() + MIME_SUFFIX; 119 } 120 121 124 private String getSystemDotKeysFilePath(Association assoc) { 125 return GNOME_SYSTEM_MIME_INFO_DIR + assoc.getName() + KEYS_SUFFIX; 126 } 127 128 131 private String getSystemDotApplicationsFilePath(Association assoc) { 132 return GNOME_SYSTEM_APPLICATION_REGISTRY_DIR + assoc.getName() + APPLICATIONS_SUFFIX; 133 } 134 135 138 private String getUserDotMimeFilePath(Association assoc) { 139 return GNOME_USER_MIME_INFO_DIR + assoc.getName() + MIME_SUFFIX; 140 } 141 142 145 private String getUserDotKeysFilePath(Association assoc) { 146 return GNOME_USER_MIME_INFO_DIR + assoc.getName() + KEYS_SUFFIX; 147 } 148 149 152 private String getUserDotApplicationsFilePath(Association assoc) { 153 return GNOME_USER_APPLICATION_INFO_DIR + assoc.getName() + APPLICATIONS_SUFFIX; 154 } 155 156 164 private void checkSystemMIMEDatabase() throws IOException { 165 File tempFile = null; 166 167 tempFile = new File (GNOME_SYSTEM_MIME_INFO_DIR); 169 if (!tempFile.exists()) { 170 throw new IOException ("The system MIME info directory doesn't exist: " 171 + GNOME_SYSTEM_MIME_INFO_DIR 172 + ". Make sure Gnome 2.0+ is installed and env GNOMEDIR is set properly."); 173 } 174 175 tempFile = new File (GNOME_SYSTEM_APPLICATION_REGISTRY_DIR); 176 if (!tempFile.exists()) { 177 throw new IOException ("The system MIME info directory doesn't exist: " 178 + GNOME_SYSTEM_APPLICATION_REGISTRY_DIR 179 + ". Make sure Gnome 2.0+ is installed and env GNOMEDIR is set properly."); 180 } 181 182 tempFile = new File (GNOME_SYSTEM_MIME_INFO_DIR); 184 if (!tempFile.canWrite()) { 185 throw new IOException ("No write permission to the system MIME info directory: " 186 + GNOME_SYSTEM_MIME_INFO_DIR); 187 } 188 189 tempFile = new File (GNOME_SYSTEM_APPLICATION_REGISTRY_DIR); 190 if (!tempFile.canWrite()) { 191 throw new IOException ("No write permission to the system MIME info directory: " 192 + GNOME_SYSTEM_APPLICATION_REGISTRY_DIR); 193 } 194 } 195 196 204 private void checkUserMIMEDatabase() throws IOException { 205 File tempFile = null; 206 207 tempFile = new File (GNOME_USER_MIME_INFO_DIR); 209 if (!tempFile.exists()) { 210 boolean mksucceed = tempFile.mkdirs(); 212 if (mksucceed == false) { 213 throw new IOException ("The user MIME info directory doesn't exist, " 214 + "and fails to be created: " + GNOME_USER_MIME_INFO_DIR); 215 } 216 } 217 218 tempFile = new File (GNOME_USER_APPLICATION_INFO_DIR); 219 if (!tempFile.exists()) { 220 boolean mksucceed = tempFile.mkdirs(); 222 if (mksucceed == false) { 223 throw new IOException ("The user MIME info directory doesn't exist, " 224 + "and fails to be created: " + GNOME_USER_APPLICATION_INFO_DIR); 225 } 226 } 227 228 tempFile = new File (GNOME_USER_MIME_INFO_DIR); 230 if (!tempFile.canWrite()) { 231 throw new IOException ("No write permission to the user MIME info directory: " 232 + GNOME_USER_MIME_INFO_DIR); 233 } 234 235 tempFile = new File (GNOME_USER_APPLICATION_INFO_DIR); 236 if (!tempFile.canWrite()) { 237 throw new IOException ("No write permission to the user MIME info directory: " 238 + GNOME_USER_MIME_INFO_DIR); 239 } 240 } 241 242 243 248 private void createFile(String mimeFilePath) throws IOException { 249 boolean createSucceed = false; 250 251 File mimeFile = new File (mimeFilePath); 252 if (!mimeFile.exists()) { 253 createSucceed = mimeFile.createNewFile(); 254 if (!createSucceed) { 255 throw new IOException ("Create MIME file: " + mimeFilePath + " failed."); 256 } 257 } 258 } 259 260 265 private void parseOpenAction(Association assoc) { 266 List actionList = assoc.getActionList(); 267 268 if (actionList == null) { 269 return; 270 } else { 271 String verb = null; 272 Iterator actionIter = actionList.iterator(); 273 274 while (actionIter.hasNext() && defaultAppCommand == null) { 275 Action oneAction = (Action) actionIter.next(); 276 verb = oneAction.getVerb(); 277 if (verb.equalsIgnoreCase("open")) { 278 defaultAppCommand = oneAction.getCommand().trim(); 279 } 280 } 281 282 if (defaultAppCommand != null) { 283 int sepIndex = defaultAppCommand.lastIndexOf(File.separator); 285 if (sepIndex == -1 || sepIndex == defaultAppCommand.length() - 1 ) { 286 defaultAppID = defaultAppCommand; 287 } else { 288 defaultAppID = defaultAppCommand.substring(sepIndex + 1, defaultAppCommand.length()); 289 } 290 } 291 } 292 } 293 294 301 private void writeDotMimeFile(Association assoc, String dotMimeFilePath) throws IOException { 302 createFile(dotMimeFilePath); 304 305 String mimeType = assoc.getMimeType(); 306 List fileExtList = assoc.getFileExtList(); 307 308 BufferedWriter mBufferWriter = null; 309 try { 310 mBufferWriter = new BufferedWriter (new FileWriter (dotMimeFilePath, true)); 312 313 mBufferWriter.write(mimeType + "\n"); 314 String fileExtensionString = null; 315 316 if (fileExtList == null) { 317 fileExtensionString = ""; 318 } else { 319 fileExtensionString = fileExtListToString(fileExtList); 320 } 321 mBufferWriter.write("\t" + "ext: " + fileExtensionString + "\n"); 322 mBufferWriter.write("\n"); 323 } catch (IOException e) { 324 throw new IOException ("Write mime info to " + dotMimeFilePath + " failed."); 325 } finally { 326 if (mBufferWriter != null) { 328 try { 329 mBufferWriter.close(); 330 } catch (IOException e) { 331 } 332 } 333 } 334 } 335 336 342 private void writeDotKeysFile(Association assoc, String dotKeysFilePath) 343 throws IOException { 344 createFile(dotKeysFilePath); 346 347 String mimeType = assoc.getMimeType(); 348 String description = assoc.getDescription(); 349 String iconFileName = assoc.getIconFileName(); 350 351 BufferedWriter kBufferWriter = null; 352 try { 353 kBufferWriter = new BufferedWriter (new FileWriter (dotKeysFilePath, true)); 355 356 kBufferWriter.write(mimeType + "\n"); 357 if (description != null) { 358 kBufferWriter.write("\t" 359 + GnomeAssociationUtil.GNOME_VFS_MIME_KEY_DESCRIPTION 360 + "=" + description + "\n"); 361 } 362 363 if (iconFileName != null) { 364 kBufferWriter.write("\t" 365 + GnomeAssociationUtil.GNOME_VFS_MIME_KEY_ICON_FILENAME 366 + "=" + iconFileName + "\n"); 367 } 368 369 parseOpenAction(assoc); 371 if (defaultAppID != null) { 372 kBufferWriter.write("\t" + "default_action_type=application" + "\n"); 373 kBufferWriter.write("\t" + "default_application_id=" + defaultAppID + "\n"); 374 kBufferWriter.write("\t" + "short_list_application_user_additions=" + defaultAppID + "\n"); 375 } 376 377 kBufferWriter.write("\n"); 378 } catch (IOException e) { 379 throw new IOException ("Write mime info to " + dotKeysFilePath + " failed."); 380 } finally { 381 if (kBufferWriter != null) { 383 try { 384 kBufferWriter.close(); 385 } catch (IOException e) { 386 } 387 } 388 } 389 } 390 391 397 private void writeDotApplicationsFile(Association assoc, String dotApplicationsFilePath) 398 throws IOException { 399 createFile(dotApplicationsFilePath); 401 402 BufferedWriter mBufferWriter = null; 403 try { 404 parseOpenAction(assoc); 406 if (defaultAppID != null && defaultAppCommand != null) { 407 mBufferWriter = new BufferedWriter (new FileWriter (dotApplicationsFilePath, true)); 409 410 mBufferWriter.write(defaultAppID + "\n"); 411 mBufferWriter.write("\t" + "command=" + defaultAppCommand + "\n"); 412 mBufferWriter.write("\t" + "name=" + defaultAppID + "\n"); 413 mBufferWriter.write("\t" + "can_open_multiple_files=false" + "\n"); 414 mBufferWriter.write("\t" + "requires_terminal=false" + "\n"); 415 416 String mimeType = assoc.getMimeType(); 417 mBufferWriter.write("\t" + "mime_types=" + mimeType + "\n"); 418 419 mBufferWriter.write("\n"); 420 } 421 } catch (IOException e) { 422 throw new IOException ("Write mime info to " + dotApplicationsFilePath + " failed."); 423 } finally { 424 if (mBufferWriter != null) { 426 try { 427 mBufferWriter.close(); 428 } catch (IOException e) { 429 } 430 } 431 } 432 } 433 434 437 private boolean dotMimeFileContainsMimeType(File dotMimeFile, String mimeType) { 438 boolean isMimeTypeExist = false; 439 440 try { 441 BufferedReader mBufferReader = new BufferedReader (new FileReader (dotMimeFile)); 442 String oneLine; 443 444 while ((oneLine = mBufferReader.readLine()) != null) { 445 if (mimeType.equals(oneLine)) { 446 isMimeTypeExist = true; 447 break; 448 } 449 } 450 451 mBufferReader.close(); 452 453 return isMimeTypeExist; 454 } catch (IOException e) { 455 return false; 456 } 457 } 458 459 468 public void checkAssociationValidForRegistration(Association assoc) 469 throws IllegalArgumentException { 470 if (assoc.getName() == null || assoc.getMimeType() == null) { 471 throw new IllegalArgumentException ("The given association is invalid. It should " + 472 "specify both the name and mimeType fields to perform this operation."); 473 } 474 } 475 476 485 public void checkAssociationValidForUnregistration(Association assoc) 486 throws IllegalArgumentException { 487 if (assoc.getName() == null) { 488 throw new IllegalArgumentException ("The given association is invalid. It should " + 489 "specify the name field to perform this operation."); 490 } 491 } 492 493 503 public boolean isAssociationExist(Association assoc, int level) { 504 File dotMimeFile = null; 505 if (level == SYSTEM_LEVEL) { 506 dotMimeFile = new File (getSystemDotMimeFilePath(assoc)); 508 } else { 509 dotMimeFile = new File (getUserDotMimeFilePath(assoc)); 511 } 512 513 if (dotMimeFile.exists()) { 514 if (assoc.getMimeType() == null) { 516 return true; 517 } else { 518 return dotMimeFileContainsMimeType(dotMimeFile, assoc.getMimeType()); 519 } 520 } else { 521 return false; 522 } 523 } 524 525 536 public void registerAssociation(Association assoc, int level) 537 throws RegisterFailedException { 538 String dotMimeFilePath = null; 539 String dotKeysFilePath = null; 540 String dotApplicationsFilePath = null; 541 542 try { 543 if (level == SYSTEM_LEVEL) { 544 checkSystemMIMEDatabase(); 545 546 dotMimeFilePath = getSystemDotMimeFilePath(assoc); 547 dotKeysFilePath = getSystemDotKeysFilePath(assoc); 548 dotApplicationsFilePath = getSystemDotApplicationsFilePath(assoc); 549 } else { 550 checkUserMIMEDatabase(); 551 552 dotMimeFilePath = getUserDotMimeFilePath(assoc); 553 dotKeysFilePath = getUserDotKeysFilePath(assoc); 554 dotApplicationsFilePath = getUserDotApplicationsFilePath(assoc); 555 } 556 557 writeDotMimeFile(assoc, dotMimeFilePath); 559 560 writeDotKeysFile(assoc, dotKeysFilePath); 562 563 writeDotApplicationsFile(assoc, dotApplicationsFilePath); 565 } catch (IOException e) { 566 if (dotMimeFilePath != null ) { 568 (new File (dotMimeFilePath)).delete(); 569 } 570 if (dotKeysFilePath != null) { 571 (new File (dotKeysFilePath)).delete(); 572 } 573 if (dotApplicationsFilePath != null) { 574 (new File (dotApplicationsFilePath)).delete(); 575 } 576 577 throw new RegisterFailedException(e.getMessage()); 578 } 579 } 580 581 591 public void unregisterAssociation(Association assoc, int level) throws RegisterFailedException { 592 String dotMimeFilePath = null; 593 String dotKeysFilePath = null; 594 String dotApplicationsFilePath = null; 595 596 try { 597 if (level == SYSTEM_LEVEL) { 598 checkSystemMIMEDatabase(); 599 600 dotMimeFilePath = getSystemDotMimeFilePath(assoc); 601 dotKeysFilePath = getSystemDotKeysFilePath(assoc); 602 dotApplicationsFilePath = getSystemDotApplicationsFilePath(assoc); 603 } else { 604 checkUserMIMEDatabase(); 605 606 dotMimeFilePath = getUserDotMimeFilePath(assoc); 607 dotKeysFilePath = getUserDotKeysFilePath(assoc); 608 dotApplicationsFilePath = getUserDotApplicationsFilePath(assoc); 609 } 610 611 (new File (dotMimeFilePath)).delete(); 613 (new File (dotKeysFilePath)).delete(); 614 (new File (dotApplicationsFilePath)).delete(); 615 } catch (IOException e) { 616 throw new RegisterFailedException(e.getMessage()); 617 } 618 } 619 } | Popular Tags |