TDE core libraries
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

metatest.cpp 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #include <tdeapplication.h>
  2. #include <tdefilemetainfo.h>
  3. #include <tdecmdlineargs.h>
  4. #include <tqstringlist.h>
  5. #include <tqimage.h>
  6. #include <kdebug.h>
  7. #include <tqlabel.h>
  8. #include <tqvalidator.h>
  9. #define I18N_NOOP
  10. static TDECmdLineOptions options[] =
  11. {
  12. { "+file", "File name", 0 },
  13. { "addgroup ", "Add a group to a file", 0},
  14. { "removegroup ", "Remove a group from a file", 0},
  15. { "removeitem ", "Remove the item from --group from a file", 0},
  16. { "group ", "Specify a group to work on", 0},
  17. { "validator", "Create a validator for an item and show its class", 0},
  18. { "item ", "Specify an item to work on", 0},
  19. { "add ", "Add the --item to the --group and set the value", 0},
  20. { "autogroupadd", "Automatically add a group if none is found", 0},
  21. { "set ", "Set the value of --item in --group", 0},
  22. { "groups", "list the groups of this file", 0 },
  23. { "mimetypeinfo ", "the mimetype info for a mimetype", 0 },
  24. TDECmdLineLastOption
  25. };
  26. void printKeyValues(KFileMetaInfo& info)
  27. {
  28. TQStringList l = info.preferredKeys();
  29. kdDebug() << "found " << l.size() << " keys\n";
  30. TQString s;
  31. TQStringList::Iterator it;
  32. for (it = l.begin(); it!=l.end(); ++it)
  33. {
  34. s +=" - " + *it;
  35. }
  36. kdDebug() << "keys: " << s << endl;
  37. for (it = l.begin(); it!=l.end(); ++it)
  38. {
  39. KFileMetaInfoItem item = info.item(*it);
  40. if ( item.isValid() && item.value().canCast(TQVariant::String)) {
  41. kdDebug() << item.key() << "(" << item.translatedKey() << ") -> "
  42. << item.string() << endl;
  43. }
  44. }
  45. }
  46. void printMimeTypeInfo(TQString mimetype)
  47. {
  48. const KFileMimeTypeInfo* info = KFileMetaInfoProvider::self()->mimeTypeInfo(mimetype);
  49. if (!info) return;
  50. kdDebug() << "Preferred groups:\n";
  51. kdDebug() << "=================\n";
  52. TQStringList groups = info->preferredGroups();
  53. for (TQStringList::Iterator it=groups.begin() ; it!=groups.end(); ++it)
  54. {
  55. kdDebug() << *it << endl;
  56. }
  57. kdDebug() << endl;
  58. kdDebug() << "Supported groups:\n";
  59. kdDebug() << "=================\n";
  60. groups = info->supportedGroups();
  61. for (TQStringList::Iterator it=groups.begin() ; it!=groups.end(); ++it)
  62. {
  63. kdDebug() << *it << endl;
  64. }
  65. for (TQStringList::Iterator it=groups.begin() ; it!=groups.end(); ++it)
  66. {
  67. const KFileMimeTypeInfo::GroupInfo* groupinfo = info->groupInfo(*it);
  68. kdDebug() << endl;
  69. kdDebug() << "Group \"" << *it << "\"\n";
  70. kdDebug() << "==================\n";
  71. if (!groupinfo) kdDebug() << "argh! no group info\n";
  72. kdDebug() << endl;
  73. kdDebug() << " Supported keys:\n";
  74. TQStringList keys = groupinfo->supportedKeys();
  75. if (!keys.count()) kdDebug() << " none\n";
  76. for (TQStringList::Iterator kit=keys.begin(); kit!=keys.end(); ++kit)
  77. {
  78. kdDebug() << " " << *kit << endl;
  79. const KFileMimeTypeInfo::ItemInfo* iti = groupinfo->itemInfo(*kit);
  80. kdDebug() << " Key: " << iti->key() << endl;
  81. kdDebug() << " Translated: " << iti->key() << endl;
  82. kdDebug() << " Type: " << TQVariant::typeToName(iti->type()) << endl;
  83. kdDebug() << " Unit: " << iti->unit() << endl;
  84. kdDebug() << " Hint: " << iti->hint() << endl;
  85. kdDebug() << " Attributes: " << iti->attributes() << endl;
  86. kdDebug() << " Prefix: " << iti->prefix() << endl;
  87. kdDebug() << " Suffix: " << iti->suffix() << endl;
  88. }
  89. kdDebug() << " name: " << groupinfo->name() << endl;
  90. kdDebug() << " translated: " << groupinfo->translatedName() << endl;
  91. kdDebug() << " attributes: " << groupinfo->attributes() << endl;
  92. kdDebug() << " variable keys: " << (groupinfo->supportsVariableKeys() ? "Yes" : "No") << endl;
  93. if (groupinfo->supportsVariableKeys())
  94. {
  95. const KFileMimeTypeInfo::ItemInfo* iti = groupinfo->variableItemInfo();
  96. kdDebug() << " variable key type/attr: " << TQVariant::typeToName(iti->type()) << " / " << iti->attributes() << endl;
  97. }
  98. }
  99. kdDebug() << endl;
  100. kdDebug() << "Preferred keys:\n";
  101. kdDebug() << "===============\n";
  102. TQStringList prefKeys = info->preferredKeys();
  103. if (!prefKeys.count()) kdDebug() << " none\n";
  104. for (TQStringList::Iterator kit=prefKeys.begin(); kit!=prefKeys.end(); ++kit)
  105. {
  106. kdDebug() << *kit << endl;
  107. }
  108. kdDebug() << endl;
  109. kdDebug() << "Supported keys:\n";
  110. kdDebug() << "===============\n";
  111. TQStringList supKeys = info->supportedKeys();
  112. if (!supKeys.count()) kdDebug() << " none\n";
  113. for (TQStringList::Iterator kit=supKeys.begin(); kit!=supKeys.end(); ++kit)
  114. {
  115. kdDebug() << *kit << endl;
  116. }
  117. }
  118. void addGroup(KFileMetaInfo& info, TQString group)
  119. {
  120. kdDebug() << "trying to add group " << group << endl;
  121. kdDebug() << "groups before: \n";
  122. TQStringList groups = info.groups();
  123. for (TQStringList::Iterator it=groups.begin() ; it!=groups.end(); ++it)
  124. kdDebug() << " " << *it << endl;
  125. if (info.addGroup(group))
  126. kdDebug() << "addGroup succeeded\n";
  127. else
  128. kdDebug() << "addGroup failed\n";
  129. kdDebug() << "trying another addGroup to see what happens\n";
  130. if (info.addGroup(group))
  131. kdDebug() << "addGroup succeeded\n";
  132. else
  133. kdDebug() << "addGroup failed\n";
  134. kdDebug() << "and afterwards: \n";
  135. groups = info.groups();
  136. for (TQStringList::Iterator it=groups.begin() ; it!=groups.end(); ++it)
  137. kdDebug() << " " << *it << endl;
  138. }
  139. void removeGroup(KFileMetaInfo& info, TQString group)
  140. {
  141. kdDebug() << "trying to remove group " << group << endl;
  142. kdDebug() << "groups before: \n";
  143. TQStringList groups = info.groups();
  144. for (TQStringList::Iterator it=groups.begin() ; it!=groups.end(); ++it)
  145. kdDebug() << " " << *it << endl;
  146. info.removeGroup(group);
  147. kdDebug() << "and afterwards: \n";
  148. groups = info.groups();
  149. for (TQStringList::Iterator it=groups.begin() ; it!=groups.end(); ++it)
  150. kdDebug() << " " << *it << endl;
  151. }
  152. int main( int argc, char **argv )
  153. {
  154. // Initialize command line args
  155. TDECmdLineArgs::init(argc, argv, "tdefilemetatest", "testing kfilmetainfo", "X");
  156. // Tell which options are supported
  157. TDECmdLineArgs::addCmdLineOptions( options );
  158. // Add options from other components
  159. TDEApplication::addCmdLineOptions();
  160. TDEApplication app;
  161. TDECmdLineArgs* args = TDECmdLineArgs::parsedArgs();
  162. TQCString ov;
  163. ov = args->getOption("mimetypeinfo");
  164. if (ov)
  165. {
  166. printMimeTypeInfo(ov);
  167. return 0;
  168. }
  169. if (!args->count()) return 1;
  170. KFileMetaInfo info( args->url(0), TQString::null, KFileMetaInfo::Everything);
  171. if (args->isSet("groups"))
  172. {
  173. TQStringList groups = info.groups();
  174. for (TQStringList::Iterator it=groups.begin() ; it!=groups.end(); ++it)
  175. {
  176. kdDebug() << "group " << *it << endl;
  177. }
  178. return 0;
  179. }
  180. TQString group, item;
  181. ov = args->getOption("addgroup");
  182. if (ov) addGroup(info, ov);
  183. ov = args->getOption("removegroup");
  184. if (ov) removeGroup(info, ov);
  185. ov = args->getOption("group");
  186. if (ov) group = ov;
  187. ov = args->getOption("item");
  188. if (ov) item = ov;
  189. ov = args->getOption("add");
  190. if (ov && !group.isNull() && !item.isNull())
  191. {
  192. KFileMetaInfoGroup g = info[group];
  193. if (!g.isValid() && args->isSet("autogroupadd"))
  194. {
  195. kdDebug() << "group is not there, adding it\n";
  196. info.addGroup(group);
  197. }
  198. // add the item
  199. KFileMetaInfoItem i = info[group].addItem(item);
  200. if (i.isValid())
  201. kdDebug() << "additem success\n";
  202. else
  203. kdDebug() << "additem failed\n";
  204. if (i.setValue(ov))
  205. kdDebug() << "setValue success\n";
  206. else
  207. kdDebug() << "setValue failed\n";
  208. }
  209. ov = args->getOption("set");
  210. if (ov && !group.isNull() && !item.isNull())
  211. {
  212. if (info[group][item].setValue(TQString(ov)))
  213. kdDebug() << "setValue success\n";
  214. else
  215. kdDebug() << "setValue failed\n";
  216. }
  217. ov = args->getOption("removeitem");
  218. if (ov && !group.isNull())
  219. {
  220. if (info[group].removeItem(ov))
  221. kdDebug() << "removeitem success\n";
  222. else
  223. kdDebug() << "removeitem failed\n";
  224. }
  225. if (args->isSet("validator") && !group.isNull() && !item.isNull())
  226. {
  227. const KFileMimeTypeInfo* kfmti = KFileMetaInfoProvider::self()->mimeTypeInfo(info.mimeType());
  228. TQValidator* v = kfmti->createValidator(group, item);
  229. if (!v)
  230. kdDebug() << "got no validator\n";
  231. else
  232. {
  233. kdDebug() << "validator is a " << v->className() << endl;
  234. delete v;
  235. }
  236. }
  237. kdDebug() << "is it valid?\n";
  238. if (!info.isValid()) return 1;
  239. kdDebug() << "it is!\n";
  240. printKeyValues(info);
  241. kdDebug() << "========= again after applyChanges() =========\n";
  242. info.applyChanges();
  243. printKeyValues(info);
  244. KFileMetaInfoItem thumbitem = info.item(KFileMimeTypeInfo::Thumbnail);
  245. // KFileMetaInfoItem thumbitem = info.item("Thumbnail");
  246. if (!thumbitem.isValid()) kdDebug() << "no thumbnail\n";
  247. else
  248. kdDebug() << "type of thumbnail is " << thumbitem.value().typeName() << endl;
  249. if (thumbitem.isValid() && thumbitem.value().canCast(TQVariant::Image))
  250. {
  251. TQLabel* label = new TQLabel(0);
  252. app.setMainWidget(label);
  253. TQPixmap pix;
  254. pix.convertFromImage(thumbitem.value().toImage());
  255. label->setPixmap(pix);
  256. label->show();
  257. app.exec();
  258. }
  259. return 0;
  260. }