~ubuntu-branches/ubuntu/natty/kdenetwork/natty-proposed

« back to all changes in this revision

Viewing changes to filesharing/advanced/propsdlgplugin/model.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-02-21 14:26:58 UTC
  • Revision ID: package-import@ubuntu.com-20110221142658-mzt9flk82tzdunxj
Tags: 4:4.6.0-0ubuntu4
Update kubuntu_05_samba_sharing.diff to match master

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <QDebug>
2
 
 
3
 
#include <kuser.h>
4
 
 
5
 
#include <sys/stat.h>
6
 
 
7
 
#include "model.h"
8
 
 
9
 
UserPermissionModel::UserPermissionModel(KSambaShareData &shareData, QObject *parent)
10
 
    : QAbstractTableModel(parent)
11
 
    , userList(getUsersList())
12
 
    , shareData(shareData)
13
 
    , usersAcl()
14
 
{
15
 
    setupData();
16
 
}
17
 
 
18
 
void UserPermissionModel::setupData()
19
 
{
20
 
    QStringList acl = shareData.acl().split(",", QString::SkipEmptyParts);
21
 
 
22
 
    QList<QString>::const_iterator itr;
23
 
    for (itr = acl.constBegin(); itr != acl.constEnd(); ++itr) {
24
 
        QStringList userInfo = (*itr).trimmed().split(":");
25
 
        usersAcl.insert(userInfo.at(0), QVariant(userInfo.at(1)));
26
 
    }
27
 
}
28
 
 
29
 
QStringList UserPermissionModel::getUsersList() const
30
 
{
31
 
    unsigned int defminuid;
32
 
    unsigned int defmaxuid;
33
 
 
34
 
#ifdef __linux__
35
 
    struct stat st;
36
 
    if (!stat("/etc/debian_version", &st)) { /* debian */
37
 
        defminuid = 1000;
38
 
        defmaxuid = 29999;
39
 
    } else if (!stat("/usr/portage", &st)) { /* gentoo */
40
 
        defminuid = 1000;
41
 
        defmaxuid = 65000;
42
 
    } else if (!stat("/etc/mandrake-release", &st)) { /* mandrake - check before redhat! */
43
 
        defminuid = 500;
44
 
        defmaxuid = 65000;
45
 
    } else if (!stat("/etc/redhat-release", &st)) { /* redhat */
46
 
        defminuid = 100;
47
 
        defmaxuid = 65000;
48
 
    } else /* if (!stat("/etc/SuSE-release", &st)) */ { /* suse */
49
 
        defminuid = 500;
50
 
        defmaxuid = 65000;
51
 
    }
52
 
#else
53
 
    defminuid = 1000;
54
 
    defmaxuid = 65000;
55
 
#endif
56
 
 
57
 
    QStringList userList;
58
 
    userList.append("Everyone");
59
 
    foreach (const QString &username, KUser::allUserNames()) {
60
 
        if (username == "nobody") {
61
 
            continue;
62
 
        }
63
 
        KUser user(username);
64
 
        if (user.uid() >= defminuid) {
65
 
            userList << username;
66
 
        }
67
 
    }
68
 
 
69
 
    return userList;
70
 
}
71
 
 
72
 
int UserPermissionModel::rowCount(const QModelIndex &parent) const
73
 
{
74
 
    return userList.count();
75
 
}
76
 
 
77
 
int UserPermissionModel::columnCount(const QModelIndex &parent) const
78
 
{
79
 
    return 2;
80
 
}
81
 
 
82
 
QVariant UserPermissionModel::data(const QModelIndex &index, int role) const
83
 
{
84
 
    if ((role == Qt::DisplayRole) && (index.column() == 0)) {
85
 
        return QVariant(userList.at(index.row()));
86
 
    }
87
 
 
88
 
    if ((role == Qt::EditRole) && (index.column() == 1)) {
89
 
        QMap<QString, QVariant>::ConstIterator itr;
90
 
        for (itr = usersAcl.constBegin(); itr != usersAcl.constEnd(); ++itr) {
91
 
            if (itr.key().endsWith(userList.at(index.row()))) {
92
 
                return itr.value();
93
 
            }
94
 
        }
95
 
    }
96
 
 
97
 
    return QVariant();
98
 
}
99
 
 
100
 
Qt::ItemFlags UserPermissionModel::flags(const QModelIndex &index) const
101
 
{
102
 
    if (index.column() == 0) {
103
 
        return Qt::ItemIsSelectable;
104
 
    }
105
 
 
106
 
    if (index.column() == 1) {
107
 
        return (Qt::ItemIsEnabled | Qt::ItemIsEditable);
108
 
    }
109
 
 
110
 
    return Qt::NoItemFlags;
111
 
}
112
 
 
113
 
bool UserPermissionModel::setData(const QModelIndex &index, const QVariant &value, int role)
114
 
{
115
 
    if ((role != Qt::EditRole) || (index.column() != 1)) {
116
 
        return false;
117
 
    }
118
 
 
119
 
    QString key("");
120
 
    QMap<QString, QVariant>::ConstIterator itr;
121
 
    for (itr = usersAcl.constBegin(); itr != usersAcl.constEnd(); ++itr) {
122
 
        if (itr.key().endsWith(userList.at(index.row()))) {
123
 
            key = itr.key();
124
 
            break;
125
 
        }
126
 
    }
127
 
 
128
 
    if (key.isEmpty()) {
129
 
        key = userList.at(index.row());
130
 
    }
131
 
 
132
 
    if (value.isNull()) {
133
 
        usersAcl.take(key);
134
 
    } else {
135
 
        usersAcl.insert(key, value);
136
 
    }
137
 
 
138
 
    emit dataChanged(index, index);
139
 
    return true;
140
 
}
141
 
 
142
 
QString UserPermissionModel::getAcl() const
143
 
{
144
 
    QString result("");
145
 
 
146
 
    QMap<QString, QVariant>::ConstIterator itr;
147
 
    for (itr = usersAcl.constBegin(); itr != usersAcl.constEnd(); ++itr) {
148
 
        if (!itr.value().toString().isEmpty()) {
149
 
            result.append(itr.key() + ":" + itr.value().toString().toLower());
150
 
            if (itr != (usersAcl.constEnd() - 1)) {
151
 
                result.append(",");
152
 
            }
153
 
        }
154
 
    }
155
 
 
156
 
    return result;
157
 
}