~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kinfocenter/Modules/samba/kcmsambalog.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * kcmsambalog.cpp
 
3
 *
 
4
 * Copyright (c) 2000 Alexander Neundorf <neundorf@kde.org>
 
5
 *
 
6
 * Requires the Qt widget libraries, available at no cost at
 
7
 * http://www.troll.no/
 
8
 *
 
9
 *  This program is free software; you can redistribute it and/or modify
 
10
 *  it under the terms of the GNU General Public License as published by
 
11
 *  the Free Software Foundation; either version 2 of the License, or
 
12
 *  (at your option) any later version.
 
13
 *
 
14
 *  This program is distributed in the hope that it will be useful,
 
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
 *  GNU General Public License for more details.
 
18
 *
 
19
 *  You should have received a copy of the GNU General Public License
 
20
 *  along with this program; if not, write to the Free Software
 
21
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
22
 */
 
23
 
 
24
#include <QApplication>
 
25
#include <QFile>
 
26
#include <QLayout>
 
27
 
 
28
#include <kmessagebox.h>
 
29
#include <kdialog.h>
 
30
#include <klocale.h>
 
31
 
 
32
#include "kcmsambalog.h"
 
33
#include "kcmsambalog.moc"
 
34
 
 
35
#define LOG_SCREEN_XY_OFFSET 10
 
36
 
 
37
LogView::LogView(QWidget *parent, KConfig *config) :
 
38
        QWidget(parent), configFile(config), filesCount(0), connectionsCount(0), logFileName(KUrl::fromPath("/var/log/samba.log"), this), label(i18n("Samba log file: "), this), viewHistory(this), showConnOpen(i18n("Show opened connections"), this), showConnClose(i18n("Show closed connections"), this),
 
39
                        showFileOpen(i18n("Show opened files"), this), showFileClose(i18n("Show closed files"), this), updateButton(i18n("&Update"), this) {
 
40
        label.setBuddy( &logFileName);
 
41
        QVBoxLayout *mainLayout=new QVBoxLayout(this);
 
42
        mainLayout->setMargin(KDialog::marginHint());
 
43
        mainLayout->setSpacing(KDialog::spacingHint());
 
44
        QHBoxLayout *leLayout=new QHBoxLayout();
 
45
        mainLayout->addItem(leLayout);
 
46
        leLayout->addWidget(&label);
 
47
        leLayout->addWidget(&logFileName, 1);
 
48
        mainLayout->addWidget(&viewHistory, 1);
 
49
        QGridLayout *subLayout=new QGridLayout();
 
50
        mainLayout->addItem(subLayout);
 
51
        subLayout->addWidget(&showConnOpen, 0, 0);
 
52
        subLayout->addWidget(&showConnClose, 1, 0);
 
53
        subLayout->addWidget(&showFileOpen, 0, 1);
 
54
        subLayout->addWidget(&showFileClose, 1, 1);
 
55
        mainLayout->addWidget(&updateButton, 0, Qt::AlignLeft);
 
56
 
 
57
        logFileName.setWhatsThis(i18n("This page presents the contents of"
 
58
                " your samba log file in a friendly layout. Check that the correct log"
 
59
                " file for your computer is listed here. If you need to, correct the name"
 
60
                " or location of the log file, and then click the \"Update\" button.") );
 
61
 
 
62
        showConnOpen.setWhatsThis(i18n("Check this option if you want to"
 
63
                " view the details for connections opened to your computer.") );
 
64
 
 
65
        showConnClose.setWhatsThis(i18n("Check this option if you want to"
 
66
                " view the events when connections to your computer were closed.") );
 
67
 
 
68
        showFileOpen.setWhatsThis(i18n("Check this option if you want to"
 
69
                " see the files which were opened on your computer by remote users."
 
70
                " Note that file open/close events are not logged unless the samba"
 
71
                " log level is set to at least 2 (you cannot set the log level"
 
72
                " using this module).") );
 
73
 
 
74
        showFileClose.setWhatsThis(i18n("Check this option if you want to"
 
75
                " see the events when files opened by remote users were closed."
 
76
                " Note that file open/close events are not logged unless the samba"
 
77
                " log level is set to at least 2 (you cannot set the log level"
 
78
                " using this module).") );
 
79
 
 
80
        updateButton.setWhatsThis(i18n("Click here to refresh the information"
 
81
                " on this page. The log file (shown above) will be read to obtain the"
 
82
                " events logged by samba.") );
 
83
 
 
84
        viewHistory.setAllColumnsShowFocus(true);
 
85
        viewHistory.setFocusPolicy(Qt::ClickFocus);
 
86
        viewHistory.setShowSortIndicator(true);
 
87
 
 
88
        viewHistory.addColumn(i18n("Date & Time"), 130);
 
89
        viewHistory.addColumn(i18n("Event"), 150);
 
90
        viewHistory.addColumn(i18n("Service/File"), 210);
 
91
        viewHistory.addColumn(i18n("Host/User"), 150);
 
92
 
 
93
        viewHistory.setWhatsThis(i18n("<p>This list shows details of the events"
 
94
                " logged by samba. Note that events at the file level are not logged"
 
95
                " unless you have configured the log level for samba to 2 or greater.</p><p>"
 
96
                " As with many other lists in KDE, you can click on a column heading"
 
97
                " to sort on that column. Click again to change the sorting direction"
 
98
                " from ascending to descending or vice versa.</p><p>"
 
99
                " If the list is empty, try clicking the \"Update\" button. The samba"
 
100
                " log file will be read and the list refreshed.</p>") );
 
101
 
 
102
        showConnOpen.setChecked(true);
 
103
        showConnClose.setChecked(true);
 
104
        showFileOpen.setChecked(false);
 
105
        showFileClose.setChecked(false);
 
106
 
 
107
        connect(&updateButton, SIGNAL(clicked()), this, SLOT(updateList()));
 
108
emit    contentsChanged(&viewHistory, 0, 0);
 
109
 
 
110
        label.setMinimumSize(label.sizeHint());
 
111
        logFileName.setMinimumSize(250, logFileName.sizeHint().height());
 
112
        viewHistory.setMinimumSize(425, 200);
 
113
        showConnOpen.setMinimumSize(showConnOpen.sizeHint());
 
114
        showConnClose.setMinimumSize(showConnClose.sizeHint());
 
115
        showFileOpen.setMinimumSize(showFileOpen.sizeHint());
 
116
        showFileClose.setMinimumSize(showFileClose.sizeHint());
 
117
        updateButton.setFixedSize(updateButton.sizeHint());
 
118
}
 
119
 
 
120
void LogView::loadSettings() {
 
121
        if (configFile==0)
 
122
                return;
 
123
        KConfigGroup group = configFile->group(LOGGROUPNAME);
 
124
        logFileName.setUrl(KUrl(group.readPathEntry("SambaLogFile", "/var/log/samba.log")));
 
125
 
 
126
        showConnOpen.setChecked(group.readEntry("ShowConnectionOpen", true));
 
127
        showConnClose.setChecked(group.readEntry("ShowConnectionClose", false));
 
128
        showFileOpen.setChecked(group.readEntry("ShowFileOpen", true));
 
129
        showFileClose.setChecked(group.readEntry("ShowFileClose", false));
 
130
}
 
131
 
 
132
void LogView::saveSettings() {
 
133
        if (configFile==0)
 
134
                return;
 
135
        KConfigGroup group = configFile->group(LOGGROUPNAME);
 
136
        group.writePathEntry("SambaLogFile", logFileName.url().path());
 
137
 
 
138
        group.writeEntry("ShowConnectionOpen", showConnOpen.isChecked());
 
139
        group.writeEntry("ShowConnectionClose", showConnClose.isChecked());
 
140
        group.writeEntry("ShowFileOpen", showFileOpen.isChecked());
 
141
        group.writeEntry("ShowFileClose", showFileClose.isChecked());
 
142
}
 
143
 
 
144
#define CONN_OPEN " connect to service "
 
145
#define CONN_CLOSE " closed connection to service "
 
146
#define FILE_OPEN " opened file "
 
147
#define FILE_CLOSE " closed file "
 
148
 
 
149
//caution ! high optimized code :-)
 
150
void LogView::updateList() {
 
151
        QFile logFile(logFileName.url().path());
 
152
        if (logFile.open(QIODevice::ReadOnly)) {
 
153
                QApplication::setOverrideCursor(Qt::WaitCursor);
 
154
                viewHistory.clear();
 
155
                filesCount=0;
 
156
                connectionsCount=0;
 
157
 
 
158
                int connOpenLen(strlen(CONN_OPEN));
 
159
                int connCloseLen(strlen(CONN_CLOSE));
 
160
                int fileOpenLen(strlen(FILE_OPEN));
 
161
                int fileCloseLen(strlen(FILE_CLOSE));
 
162
 
 
163
                char buf[400];
 
164
                char *c1, *c2, *c3, *c4, *c, time[25];
 
165
                int timeRead(0);
 
166
 
 
167
                while (!logFile.atEnd()) {
 
168
                        logFile.readLine(buf, sizeof(buf));
 
169
                        timeRead=0;
 
170
                        if (buf[0]=='[') {
 
171
                                if (strlen(buf)>11)
 
172
                                        if (buf[5]=='/') {
 
173
                                                buf[20]='\0';
 
174
                                                strncpy(time, buf+1, sizeof(time));
 
175
                                                time[sizeof(time)-1] = '\0';
 
176
                                                timeRead=1;
 
177
                                        };
 
178
                        };
 
179
                        if (timeRead==0) {
 
180
                                c1=0;
 
181
                                c2=0;
 
182
                                c3=0;
 
183
                                c4=0;
 
184
                                if (showConnOpen.isChecked())
 
185
                                        c1=strstr(buf, CONN_OPEN);
 
186
                                if (c1==0) {
 
187
                                        if (showConnClose.isChecked())
 
188
                                                c2=strstr(buf, CONN_CLOSE);
 
189
                                        if (c2==0) {
 
190
                                                if (showFileOpen.isChecked())
 
191
                                                        c3=strstr(buf, FILE_OPEN);
 
192
                                                if (c3==0) {
 
193
                                                        if (showFileClose.isChecked())
 
194
                                                                c4=strstr(buf, FILE_CLOSE);
 
195
                                                        if (c4==0)
 
196
                                                                continue;
 
197
                                                };
 
198
                                        };
 
199
                                };
 
200
                                if (c1!=0) {
 
201
                                        c=strstr(buf, " as user");
 
202
                                        *c='\0';
 
203
                                        *c1='\0';
 
204
                                        new QListViewItemX(&viewHistory,time,I18N_NOOP("CONNECTION OPENED"),c1+connOpenLen,buf+2);
 
205
                                        connectionsCount++;
 
206
                                } else if (c2!=0) {
 
207
                                        *c2='\0';
 
208
                                        new QListViewItemX(&viewHistory,time,I18N_NOOP("CONNECTION CLOSED"),c2+connCloseLen,buf+2);
 
209
                                } else if (c3!=0) {
 
210
                                        c=strstr(buf, " read=");
 
211
                                        *c='\0';
 
212
                                        *c3='\0';
 
213
                                        new QListViewItemX(&viewHistory,time,I18N_NOOP("            FILE OPENED"),c3+fileOpenLen,buf+2);
 
214
                                        filesCount++;
 
215
                                } else if (c4!=0) {
 
216
                                        c=strstr(buf, " (numopen=");
 
217
                                        *c='\0';
 
218
                                        *c4='\0';
 
219
                                        new QListViewItemX(&viewHistory,time,I18N_NOOP("            FILE CLOSED"),c4+fileCloseLen,buf+2);
 
220
                                };
 
221
                        };
 
222
                };
 
223
                logFile.close();
 
224
emit            contentsChanged(&viewHistory, filesCount, connectionsCount);
 
225
                QApplication::restoreOverrideCursor();
 
226
        } else {
 
227
                QString tmp = i18n("Could not open file %1", logFileName.url().path());
 
228
                KMessageBox::error(this, tmp);
 
229
        };
 
230
}
 
231