Advertisement
Guest User

Untitled

a guest
Nov 27th, 2011
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.00 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <Mime.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <Path.h>
  7. #include <Entry.h>
  8. #include <Message.h>
  9. using namespace std;
  10.  
  11. void ShowUsage()
  12. {
  13.     cout <<
  14.         "# setmime:" << endl <<
  15.         "# usage: setmime ((-dump | -dumpSniffRule | -dumpIcon | -dumpAll) [ <signatureString> ] )" << endl <<
  16.         "#      | (-remove <signatureString> )" << endl <<
  17.         "#      | ( (-set | -force | -add)  <signatureString>" << endl <<
  18.         "#          [ -short <short description> ] [ -long <long description> ]" << endl <<
  19.         "#          [ -preferredApp <preferred app path> ]" << endl <<
  20.         "#          [ -preferredAppSig <preferred app signature> ]" << endl <<
  21.         "#          [ -sniffRule <sniffRule> ]" << endl <<
  22.         "#          [ -extension <file suffix> ]" << endl <<
  23.         "#          [ -attribute <internal name>" << endl <<
  24.         "#             [ -attrName <public name> ] [ -attrType <type code> ]" << endl <<
  25.         "#             [ -attrWidth <display width> ] [ -attrAlignment <position> ]" << endl <<
  26.         "#             [ -attrViewable <bool flag> ] [ -attrEditable <bool flag> ]" << endl <<
  27.         "#             [ -attrExtra <bool flag> ] ]" << endl <<
  28.         "#          [ -miniIcon <256 hex bytes> ]" << endl <<
  29.         "#          [ -largeIcon <1024 hex bytes> ] ... )" << endl <<
  30.         "#      | (-checkSniffRule <sniffRule>" << endl <<
  31.         "#      | -includeApps)" << endl;
  32. }
  33.  
  34. void ShowHelp()
  35. {
  36.     cout <<
  37.         "#  -dump prints a specified metamime" << endl <<
  38.         "#  -remove removes specified metamime" << endl <<
  39.         "#  -add adds specified metamime and specified metamime attributes" << endl <<
  40.         "#      that have not yet been defined" << endl <<
  41.         "#  -set adds specified metamime and specified metamime attributes," << endl <<
  42.         "#      overwrites the existing values of specified metamime attributes" << endl <<
  43.         "#  -force adds specified metamime and specified metamime attributes" << endl <<
  44.         "#      after first erasing all the existing attributes" << endl <<
  45.         "#  -dumpSniffRule prints just the MIME sniffer rule of a specified metamime" << endl <<
  46.         "#     -dumpIcon prints just the icon information of a specified metamime" << endl <<
  47.         "#  -dumpAll prints all the information, including icons of a specified metamime" << endl <<
  48.         "#  -checkSniffRule parses a MIME sniffer rule and reports any errors" << endl <<
  49.         "#  -includeApps will include applications"  << endl;
  50. }
  51.  
  52. int dump(const char* mimetype, const char* runpath)
  53. {
  54.     BMimeType mime;
  55.     mime.SetType(mimetype);    
  56.     if(!mime.IsInstalled() || !mime.IsValid() )
  57.     {
  58.         cout << runpath << ": incorrect signature" << endl;
  59.         ShowUsage();
  60.         return -1;
  61.     }  
  62.     char temp[B_MIME_TYPE_LENGTH]={0};
  63.    
  64.     cout << runpath << " -set " << mimetype;    
  65.     if (mime.GetShortDescription(temp) == B_OK)  
  66.         cout << " -short \"" << temp << "\"";  
  67.        
  68.     if (mime.GetLongDescription(temp) == B_OK)
  69.         cout << " -long \"" << temp << "\"";
  70.        
  71.     entry_ref prefappref;
  72.     if (mime.GetAppHint(&prefappref) == B_OK)
  73.     {
  74.         BPath prefapppath(&prefappref);
  75.         cout << " -preferredApp " << prefapppath.Path();
  76.     }  
  77.      
  78.     if (mime.GetPreferredApp(temp, B_OPEN) == B_OK)
  79.         cout << " -preferredAppSig " << temp;
  80.    
  81.     BMessage msg ;  uint32 i=0; const char* ptr;
  82.     if (mime.GetFileExtensions( &msg) == B_OK)
  83.         while(msg.FindString("extensions", i++, &ptr) == B_OK)
  84.             cout << " -extension " << ptr;
  85.            
  86.     cout << endl;
  87.     return 0;
  88. }
  89.  
  90. void dumpEverything(const char* runpath)
  91. {
  92.     BMessage msg; uint32 i=0; const char* ptr;
  93.     if (BMimeType::GetInstalledTypes(&msg) == B_OK)
  94.         while(msg.FindString("types", i++, &ptr) == B_OK)
  95.             dump(ptr, runpath);
  96. }
  97.  
  98. int remove(const char* mimetype, const char* runpath)
  99. {
  100.     BMimeType mime;
  101.     mime.SetType(mimetype);    
  102.     if(!mime.IsInstalled() || !mime.IsValid() )
  103.     {
  104.         cout << runpath << ": incorrect signature" << endl;
  105.         ShowUsage();
  106.         return -1;
  107.     }
  108.     mime.Delete();
  109.     return 0;
  110. }
  111.  
  112. int set(const int argc, char** argv)
  113. {
  114.     const char* runpath = argv[0]; const char* mimetype = argv[2];  
  115.     BMimeType mime;
  116.     mime.SetType(mimetype);
  117.     if(!mime.IsInstalled() || !mime.IsValid() )
  118.     {
  119.         cout << runpath << ": incorrect signature" << endl;
  120.         ShowUsage();
  121.         return -1;
  122.     }
  123.    
  124.     return 0;
  125. }
  126.  
  127. int main(int argc, char** argv)
  128. {  
  129.     if (argc==1)
  130.         ShowUsage();
  131.        
  132.     if (argc>1)
  133.     {
  134.         if (strcmp(argv[1], "--help")==0)  
  135.             ShowHelp();
  136.            
  137.         if (strcmp(argv[1], "-dump")==0)
  138.             if (argc==2)
  139.                 dumpEverything(argv[0]);
  140.             else
  141.                 if (argc==3)
  142.                     dump(argv[2], argv[0]);
  143.                 else
  144.                     {cout << argv[0] << ": signature already specified" << endl; ShowUsage();}
  145.        
  146.         if (strcmp(argv[1], "-remove")==0)
  147.             if(argc==3)
  148.                 remove(argv[2], argv[0]);
  149.             else
  150.                 if (argc==2) {cout << argv[0] << ": no signature specified" << endl; ShowUsage();}
  151.                 else
  152.                     {cout << argv[0] << ": signature already specified" << endl; ShowUsage();}
  153.                    
  154.         if (strcmp(argv[1], "-set")==0)
  155.             if (argc==2) {cout << argv[0] << ": no signature specified" << endl; ShowUsage();}
  156.             else
  157.                 set(argc, argv);
  158.                
  159.     }
  160.  
  161.  
  162.     return 0;
  163. }
  164.  
  165.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement