qspreply.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "qspreply.h"
  2. #include <QTimer>
  3. #include <QMimeDatabase>
  4. #include <QMimeType>
  5. #include <QFile>
  6. #include "comtools.h"
  7. QspReply::QspReply(const QUrl &url, const QString &_text, bool _isUseHtml, const QString &_path, const QColor &_linkColor, const QColor &_backColor, const QColor &_fontColor, const QString &_bmpBg, const QFont &_font)
  8. {
  9. offset = 0;
  10. m_text = _text;
  11. m_path = _path;
  12. m_bmpBg = _bmpBg;
  13. m_linkColor = _linkColor;
  14. m_backColor = _backColor;
  15. m_fontColor = _fontColor;
  16. m_font = _font;
  17. m_isUseHtml = _isUseHtml;
  18. setUrl(url);
  19. QString url_str = QByteArray::fromPercentEncoding(url.toString().toUtf8());
  20. if(url_str.compare("qsp:" , Qt::CaseInsensitive) == 0 || url_str.compare("qsp:/" , Qt::CaseInsensitive) == 0)
  21. {
  22. QString replystr;
  23. replystr.append("<html>\n<head>\n<meta charset=\"UTF-8\">\n<style>\nbody {\n");
  24. if(m_backColor.isValid())
  25. replystr.append(QString("background-color: %1;\n").arg(m_backColor.name()));
  26. if(m_fontColor.isValid())
  27. replystr.append(QString("color: %1;\n").arg(m_fontColor.name()));
  28. replystr.append(QString("font-family: %1;\n").arg(m_font.family()));
  29. replystr.append(QString("font-size: %1pt;\n").arg(m_font.pointSize()));
  30. if(!m_bmpBg.isEmpty())
  31. replystr.append(QString("background: url(%1) no-repeat center center fixed;\nbackground-size: cover;\n").arg(m_bmpBg));
  32. if(m_linkColor.isValid())
  33. replystr.append(QString("}\na:link {\ncolor: %1;\n").arg(m_linkColor.name()));
  34. replystr.append("}\n</style></head>\n<body>\n");
  35. replystr.append(m_text);
  36. replystr.append("</body>\n</html>");
  37. open(ReadOnly | Unbuffered);
  38. if(m_isUseHtml)
  39. {
  40. content = replystr.toUtf8();
  41. setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/html"));
  42. }
  43. else
  44. {
  45. setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/plain; charset=utf-8"));
  46. content = m_text.toUtf8();
  47. }
  48. }
  49. else
  50. {
  51. open(ReadOnly | Unbuffered);
  52. QString path = QSPTools::GetCaseInsensitiveFilePath(m_path, url_str.mid(5));
  53. QMimeDatabase db;
  54. QMimeType type = db.mimeTypeForFile(m_path + path);
  55. QFile file( m_path + path );
  56. if(file.open(QIODevice::ReadOnly))
  57. {
  58. content = file.readAll();
  59. setHeader(QNetworkRequest::ContentTypeHeader, QVariant(type.name().toUtf8()));
  60. }
  61. file.close();
  62. }
  63. setHeader(QNetworkRequest::ContentLengthHeader, QVariant(content.size()));
  64. QTimer::singleShot(0, this, SLOT(readyRead()));
  65. QTimer::singleShot(0, this, SLOT(finished()));
  66. //emit readyRead();
  67. //emit finished();
  68. }
  69. void QspReply::abort()
  70. {
  71. }
  72. qint64 QspReply::bytesAvailable() const
  73. {
  74. return content.size() - offset + QIODevice::bytesAvailable();
  75. }
  76. bool QspReply::isSequential() const
  77. {
  78. return true;
  79. }
  80. qint64 QspReply::readData(char *data, qint64 maxSize)
  81. {
  82. if (offset < content.size()) {
  83. qint64 number = qMin(maxSize, content.size() - offset);
  84. memcpy(data, content.constData() + offset, number);
  85. offset += number;
  86. return number;
  87. } else
  88. return -1;
  89. }