/
contentinfo.cpp
159 lines (139 loc) · 4.07 KB
/
contentinfo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* Copyright (C) 2011 Nokia Corporation.
*
* Contact: Marius Vollmer <marius.vollmer@nokia.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/
#include "contentinfo.h"
#include "internal.h"
#include <gio/gio.h>
/*!
\class ContentInfo
\brief ContentInfo provides information about a content object,
including its mime type, and a generic description and icon.
*/
struct ContentInfo::Private {
bool isValid;
QString mimeType;
QString icon;
QString description;
};
ContentInfo::ContentInfo()
: priv(new Private)
{
priv->isValid = false;
}
ContentInfo::ContentInfo(Private *priv)
: priv(priv)
{
}
ContentInfo::ContentInfo(const ContentInfo& other)
: priv(other.priv)
{
}
ContentInfo& ContentInfo::operator=(const ContentInfo& other)
{
priv = other.priv;
return *this;
}
ContentInfo::~ContentInfo()
{
}
/// Determines whether this ContentInfo is valid or not. Invalid
/// ContentInfo objects are returned when no information about a
/// object could be found.
///
/// Invalid ContentInfo instances can still be accessed, but they will
/// return empty strings for mimeType, typeDescription, and typeIcon.
bool
ContentInfo::isValid() const
{
return priv->isValid;
}
/// Returns the mime type of this content object
QString
ContentInfo::mimeType() const
{
return priv->mimeType;
}
/// Returns a one-line, localized description of the type of the
/// content object.
QString
ContentInfo::typeDescription() const
{
return priv->description;
}
/// Returns the name of an icon to represent the type of this content
/// object.
QString
ContentInfo::typeIcon() const
{
return priv->icon;
}
/// Returns information for the given mime type \a mimeType.
ContentInfo
ContentInfo::forMime(const QString &mimeType)
{
gchar *contentType = g_content_type_from_mime_type(mimeType.toUtf8());
Private *priv = new Private;
priv->isValid = true;
priv->mimeType = mimeType;
if (contentType) {
GIcon *icon = g_content_type_get_icon(contentType);
if (G_IS_THEMED_ICON(icon)) {
const gchar *const *names = g_themed_icon_get_names(G_THEMED_ICON(icon));
priv->icon = names[0];
}
g_object_unref(icon);
gchar* description = g_content_type_get_description(contentType);
// This will copy the data
priv->description = QString(description);
g_free(description);
g_free(contentType);
}
return ContentInfo(priv);
}
/// Returns information for the file identified by \a url. The file
/// does not need to exist. If it does, its content will be used to
/// guess its type; otherwise only the filename will be used.
ContentInfo
ContentInfo::forFile(const QUrl &url)
{
QString mime = ContentAction::Internal::mimeForFile(url);
if (!mime.isEmpty())
return forMime(mime);
else
return ContentInfo();
}
/// Returns information for the given \a bytes. The \a bytes are
/// assumed to be the first few bytes of a content object, and its
/// type is guessed from them.
ContentInfo
ContentInfo::forData(const QByteArray &bytes)
{
gchar *content_type = g_content_type_guess(NULL, (const guchar *)bytes.constData(), bytes.size(), NULL);
if (content_type) {
gchar* mime_type = g_content_type_get_mime_type(content_type);
ContentInfo info = forMime(mime_type);
if (mime_type != 0)
g_free(mime_type);
g_free(content_type);
return info;
} else {
return ContentInfo();
}
}