aboutsummaryrefslogtreecommitdiff
path: root/src/mimeparser.cpp
diff options
context:
space:
mode:
authorFelix (xq) Queißner <git@mq32.de>2020-06-19 00:46:36 +0200
committerFelix (xq) Queißner <git@mq32.de>2020-06-19 00:46:36 +0200
commitdd7090dda0370f35e0e0e2e17f3120d906aa2080 (patch)
tree557d9fb68beab0133785307f311b4504ddf4c895 /src/mimeparser.cpp
parenta716f0804fd10a37ccc7d87c58ffebeb4f261d10 (diff)
downloadkristall-dd7090dda0370f35e0e0e2e17f3120d906aa2080.tar.gz
Adds iconv implementation to convert between input charset and UTF-8 for display.
Diffstat (limited to 'src/mimeparser.cpp')
-rw-r--r--src/mimeparser.cpp63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/mimeparser.cpp b/src/mimeparser.cpp
new file mode 100644
index 0000000..dd254a4
--- /dev/null
+++ b/src/mimeparser.cpp
@@ -0,0 +1,63 @@
+#include "mimeparser.hpp"
+
+
+
+bool MimeType::is(const QString &type, const QString &sub_type) const
+{
+ return (this->type == type) and (this->subtype == sub_type);
+}
+
+QString MimeType::parameter(const QString &param_name, QString const & default_value) const
+{
+ auto val = parameters.value(param_name.toLower());
+ if(val.isNull())
+ val = default_value;
+ return val;
+}
+
+MimeType MimeParser::parse(const QString &mime_text)
+{
+ MimeType type;
+
+ QString arg_list;
+ QString mime_part;
+
+ if(int idx = mime_text.indexOf(' '); idx >= 0) {
+ arg_list = mime_text.mid(idx + 1).trimmed().toLower();
+ mime_part = mime_text.mid(0, idx).trimmed().toLower();
+ } else {
+ mime_part = mime_text.trimmed().toLower();
+ }
+
+ if(int idx = mime_part.indexOf('/'); idx >= 0) {
+ type.type = mime_part.mid(0, idx);
+ type.subtype = mime_part.mid(idx + 1);
+ } else {
+ type.type = mime_part;
+ type.subtype = QString { };
+ }
+
+ if(not arg_list.isEmpty()) {
+ for(auto const & _arg : arg_list.split(';'))
+ {
+ QString arg = _arg.trimmed();
+ if(arg.isEmpty()) // skip over double spaces
+ continue;
+
+ QString key;
+ QString value;
+
+ if(int idx = arg.indexOf('='); idx >= 0) {
+ key = arg.mid(0, idx);
+ value = arg.mid(idx + 1);
+ } else {
+ key = arg;
+ value = "";
+ }
+
+ type.parameters.insert(key, value);
+ }
+ }
+
+ return type;
+}