redak.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* #ident "$Id: $"
  2. * @author: rzr@gna.org - rev: $Author: rzr$
  3. * Copyright: See README file that comes with this distribution
  4. *****************************************************************************/
  5. #include "config.h"
  6. #include "redak.h"
  7. #include <QtPlugin>
  8. #include <QProcess>
  9. Redak::Redak(QDeclarativeItem *parent)
  10. : QDeclarativeItem(parent)
  11. //: QDeclarativeExtensionPlugin(parent)
  12. {
  13. FUNCT();
  14. }
  15. void Redak::registerTypes(const char *uri)
  16. {
  17. FUNCT();
  18. qDebug()<<uri;
  19. // Q_ASSERT(uri == QLatin1String("redak"));
  20. uri="redak";
  21. qmlRegisterType<Redak>(uri, 1, 1, "Redak");
  22. }
  23. bool Redak::save(QString content, QString filename)
  24. {
  25. FUNCT();
  26. bool status = true;
  27. QUrl url(filename);
  28. filename = url.path();
  29. #if defined Q_OS_SYMBIAN && !defined Q_WS_SIMULATOR
  30. filename = filename.mid(1); //TODO WORAROUND BUG
  31. #endif
  32. // qDebug()<<"save:" + filename;
  33. // qDebug()<<"content:" + content;
  34. QFile file( filename );
  35. status &= file.open(QIODevice::WriteOnly | QIODevice::Text);
  36. status &= file.isWritable();
  37. if ( status ) {
  38. QTextStream stream( &file );
  39. stream<<content;
  40. file.close();
  41. emit saved();
  42. }
  43. if (!status) {
  44. QString text = "error: io: save: "+filename;
  45. // qDebug()<< text;
  46. emit error(QVariant(text));
  47. }
  48. return status;
  49. }
  50. QString Redak::load(QString filename)
  51. {
  52. FUNCT();
  53. // qDebug()<<"load: " + filename;
  54. QString content = "";
  55. bool status = true;
  56. QUrl url(filename);
  57. filename = url.path();
  58. #if defined Q_OS_SYMBIAN && !defined Q_WS_SIMULATOR
  59. filename = filename.mid(1); //TODO WORAROUND BUG
  60. #endif
  61. QFile file(filename); //TODO: on dir ?
  62. if ( file.exists() ) {
  63. status &= file.open(QIODevice::ReadOnly | QIODevice::Text);
  64. QTextStream stream( &file );
  65. content = stream.readAll();
  66. file.close();
  67. emit loaded(content);
  68. } else {
  69. status &= file.open(QIODevice::WriteOnly | QIODevice::Text);
  70. content = "# file://" + filename + "\n";
  71. file.close();
  72. emit loaded(content);
  73. }
  74. if (!status) {
  75. QString text = "error: io: load: "+filename;
  76. emit error(QVariant(text));
  77. }
  78. return content;
  79. }
  80. QString Redak::process(QString const filename)
  81. {
  82. QProcess tmp;
  83. tmp.start(filename);
  84. tmp.closeWriteChannel();
  85. tmp.waitForFinished();
  86. QString content = tmp.readAllStandardOutput();
  87. return (content);
  88. }
  89. Q_EXPORT_PLUGIN2(Redak, Redak);
  90. //qmlRegisterType<Redak>("Redak", 1, 0, "Redak");