KDEsu
kcookie.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "kcookie.h"
00014
00015 #include <stdlib.h>
00016
00017 #include <QtCore/QString>
00018 #include <QtCore/QStringList>
00019 #include <QtCore/QBool>
00020 #include <QtCore/Q_PID>
00021
00022 #include <kdebug.h>
00023
00024 namespace KDESu {
00025 namespace KDESuPrivate {
00026
00027 class KCookie::KCookiePrivate
00028 {
00029 public:
00030 QByteArray m_Display;
00031 #ifdef Q_WS_X11
00032 QByteArray m_DisplayAuth;
00033 #endif
00034 };
00035
00036
00037
00038 KCookie::KCookie()
00039 : d( new KCookiePrivate )
00040 {
00041 #ifdef Q_WS_X11
00042 getXCookie();
00043 #endif
00044 }
00045
00046 KCookie::~KCookie()
00047 {
00048 delete d;
00049 }
00050
00051 QByteArray KCookie::display() const
00052 {
00053 return d->m_Display;
00054 }
00055
00056 #ifdef Q_WS_X11
00057 QByteArray KCookie::displayAuth() const
00058 {
00059 return d->m_DisplayAuth;
00060 }
00061 #endif
00062
00063 void KCookie::getXCookie()
00064 {
00065 #ifdef Q_WS_X11
00066 d->m_Display = qgetenv("DISPLAY");
00067 #else
00068 d->m_Display = qgetenv("QWS_DISPLAY");
00069 #endif
00070 if (d->m_Display.isEmpty())
00071 {
00072 kError(900) << k_lineinfo << "$DISPLAY is not set.\n";
00073 return;
00074 }
00075 #ifdef Q_WS_X11 // No need to mess with X Auth stuff
00076 QByteArray disp = d->m_Display;
00077 if (disp.startsWith("localhost:"))
00078 disp.remove(0, 9);
00079
00080 QProcess proc;
00081 proc.start("xauth", QStringList() << "list" << disp);
00082 if (!proc.waitForStarted())
00083 {
00084 kError(900) << k_lineinfo << "Could not run xauth.\n";
00085 return;
00086 }
00087 proc.waitForReadyRead(100);
00088 QByteArray output = proc.readLine().simplified();
00089 if (output.isEmpty())
00090 {
00091 kWarning(900) << "No X authentication info set for display " <<
00092 d->m_Display << endl; return;
00093 }
00094 QList<QByteArray> lst = output.split(' ');
00095 if (lst.count() != 3)
00096 {
00097 kError(900) << k_lineinfo << "parse error.\n";
00098 return;
00099 }
00100 d->m_DisplayAuth = (lst[1] + ' ' + lst[2]);
00101 proc.waitForFinished(100);
00102 #endif
00103 }
00104
00105 }}