• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KIO

  • kio
  • kio
udsentry.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 Copyright (C) 2000-2005 David Faure <faure@kde.org>
3 Copyright (C) 2007 Norbert Frese <nf2@scheinwelt.at>
4 Copyright (C) 2007 Thiago Macieira <thiago@kde.org>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20*/
21
22#include "udsentry.h"
23
24#include <kdebug.h>
25#include <kio/global.h>
26#include <kio/kio_export.h>
27
28#include <QtCore/QString>
29#include <QtCore/QHash>
30#include <QtCore/QList>
31
32using namespace KIO;
33
34/* ---------- UDSEntry ------------ */
35
36class KIO::UDSEntryPrivate : public QSharedData
37{
38public:
39 struct Field
40 {
41 inline Field() : m_long(0) { }
42 QString m_str;
43 long long m_long;
44 };
45 typedef QHash<uint, Field> FieldHash;
46 FieldHash fields;
47
48 static void save(QDataStream &, const UDSEntry &);
49 static void load(QDataStream &, UDSEntry &);
50};
51Q_DECLARE_TYPEINFO(KIO::UDSEntryPrivate::Field, Q_MOVABLE_TYPE);
52
53UDSEntry::UDSEntry()
54 : d(new UDSEntryPrivate())
55{
56}
57
58UDSEntry::UDSEntry(const UDSEntry &other)
59 : d(other.d)
60{
61}
62
63UDSEntry::~UDSEntry()
64{
65}
66
67UDSEntry &UDSEntry::operator=(const UDSEntry &other)
68{
69 d = other.d;
70 return *this;
71}
72
73QString UDSEntry::stringValue(uint field) const
74{
75 return d->fields.value(field).m_str;
76}
77
78long long UDSEntry::numberValue(uint field, long long defaultValue) const
79{
80 UDSEntryPrivate::FieldHash::ConstIterator it = d->fields.find(field);
81 return it != d->fields.constEnd() ? it->m_long : defaultValue;
82}
83
84bool UDSEntry::isDir() const
85{
86 return S_ISDIR(numberValue(UDS_FILE_TYPE));
87}
88
89bool UDSEntry::isLink() const
90{
91 return !stringValue(UDS_LINK_DEST).isEmpty();
92}
93
94void UDSEntry::insert(uint field, const QString& value)
95{
96 UDSEntryPrivate::Field f;
97 f.m_str = value;
98 d->fields.insert(field, f);
99}
100
101void UDSEntry::insert(uint field, long long value)
102{
103 UDSEntryPrivate::Field f;
104 f.m_long = value;
105 d->fields.insert(field, f);
106}
107
108QList<uint> UDSEntry::listFields() const
109{
110 return d->fields.keys();
111}
112
113int UDSEntry::count() const
114{
115 return d->fields.count();
116}
117
118bool UDSEntry::contains(uint field) const
119{
120 return d->fields.contains(field);
121}
122
123bool UDSEntry::remove(uint field)
124{
125 return d->fields.remove(field) > 0;
126}
127
128void UDSEntry::clear()
129{
130 d->fields.clear();
131}
132
133QDataStream & operator<<(QDataStream &s, const UDSEntry &a)
134{
135 UDSEntryPrivate::save(s, a);
136 return s;
137}
138
139QDataStream & operator>>(QDataStream &s, UDSEntry &a)
140{
141 UDSEntryPrivate::load(s, a);
142 return s;
143}
144
145void UDSEntryPrivate::save(QDataStream &s, const UDSEntry &a)
146{
147 const FieldHash &e = a.d->fields;
148
149 s << e.size();
150 FieldHash::ConstIterator it = e.begin();
151 const FieldHash::ConstIterator end = e.end();
152 for( ; it != end; ++it)
153 {
154 const quint32 uds = it.key();
155 s << uds;
156 if (uds & KIO::UDSEntry::UDS_STRING)
157 s << it->m_str;
158 else if (uds & KIO::UDSEntry::UDS_NUMBER)
159 s << it->m_long;
160 else
161 Q_ASSERT_X(false, "KIO::UDSEntry", "Found a field with an invalid type");
162 }
163 }
164
165void UDSEntryPrivate::load(QDataStream &s, UDSEntry &a)
166{
167 FieldHash &e = a.d->fields;
168
169 e.clear();
170 quint32 size;
171 s >> size;
172
173 // We cache the loaded strings. Some of them, like, e.g., the user,
174 // will often be the same for many entries in a row. Caching them
175 // permits to use implicit sharing to save memory.
176 static QVector<QString> cachedStrings;
177 if (cachedStrings.size() < size) {
178 cachedStrings.resize(size);
179 }
180
181 for(quint32 i = 0; i < size; ++i)
182 {
183 quint32 uds;
184 s >> uds;
185 if (uds & KIO::UDSEntry::UDS_STRING) {
186 // If the QString is the same like the one we read for the
187 // previous UDSEntry at the i-th position, use an implicitly
188 // shared copy of the same QString to save memory.
189 QString buffer;
190 s >> buffer;
191
192 if (buffer != cachedStrings.at(i)) {
193 cachedStrings[i] = buffer;
194 }
195
196 Field f;
197 f.m_str = cachedStrings.at(i);
198 e.insert(uds, f);
199 } else if (uds & KIO::UDSEntry::UDS_NUMBER) {
200 Field f;
201 s >> f.m_long;
202 e.insert(uds, f);
203 } else {
204 Q_ASSERT_X(false, "KIO::UDSEntry", "Found a field with an invalid type");
205 }
206 }
207}
208
209
KIO::UDSEntry
Universal Directory Service.
Definition: udsentry.h:59
KIO::UDSEntry::stringValue
QString stringValue(uint field) const
Definition: udsentry.cpp:73
KIO::UDSEntry::numberValue
long long numberValue(uint field, long long defaultValue=0) const
Definition: udsentry.cpp:78
KIO::UDSEntry::UDSEntry
UDSEntry()
Definition: udsentry.cpp:53
KIO::UDSEntry::isLink
bool isLink() const
Definition: udsentry.cpp:89
KIO::UDSEntry::UDS_LINK_DEST
@ UDS_LINK_DEST
Name of the file where the link points to Allows to check for a symlink (don't use S_ISLNK !...
Definition: udsentry.h:184
KIO::UDSEntry::UDS_FILE_TYPE
@ UDS_FILE_TYPE
File type, part of the mode returned by stat (for a link, this returns the file type of the pointed i...
Definition: udsentry.h:181
KIO::UDSEntry::UDS_STRING
@ UDS_STRING
Indicates that the field is a QString.
Definition: udsentry.h:135
KIO::UDSEntry::UDS_NUMBER
@ UDS_NUMBER
Indicates that the field is a number (long long)
Definition: udsentry.h:137
KIO::UDSEntry::contains
bool contains(uint field) const
check existence of a field
Definition: udsentry.cpp:118
KIO::UDSEntry::insert
void insert(uint field, const QString &value)
insert field with numeric value
Definition: udsentry.cpp:94
KIO::UDSEntry::isDir
bool isDir() const
Definition: udsentry.cpp:84
KIO::UDSEntry::clear
void clear()
remove all fields
Definition: udsentry.cpp:128
KIO::UDSEntry::remove
bool remove(uint field)
remove a field with a certain numeric id
Definition: udsentry.cpp:123
KIO::UDSEntry::~UDSEntry
~UDSEntry()
Definition: udsentry.cpp:63
KIO::UDSEntry::operator=
UDSEntry & operator=(const UDSEntry &other)
Definition: udsentry.cpp:67
KIO::UDSEntry::listFields
QList< uint > listFields() const
lists all fields
Definition: udsentry.cpp:108
KIO::UDSEntry::count
int count() const
count fields
Definition: udsentry.cpp:113
QHash
QList
f
static quint32 f(DES_KEY *key, quint32 r, char *subkey)
Definition: des.cpp:378
global.h
defaultValue
QString defaultValue(const QString &t)
kdebug.h
kio_export.h
KIO
A namespace for KIO globals.
Definition: kbookmarkmenu.h:55
KIO::operator<<
QDataStream & operator<<(QDataStream &s, const AuthInfo &a)
Definition: authinfo.cpp:209
KIO::operator>>
QDataStream & operator>>(QDataStream &s, AuthInfo &a)
Definition: authinfo.cpp:219
save
KAction * save(const QObject *recvr, const char *slot, QObject *parent)
end
const KShortcut & end()
Q_DECLARE_TYPEINFO
Q_DECLARE_TYPEINFO(KIO::UDSEntryPrivate::Field, Q_MOVABLE_TYPE)
udsentry.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal