• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDED

khostname.cpp

Go to the documentation of this file.
00001 /*  This file is part of the KDE libraries
00002  *  Copyright (C) 2001 Waldo Bastian <bastian@kde.org>
00003  *
00004  *  This library is free software; you can redistribute it and/or
00005  *  modify it under the terms of the GNU Library General Public
00006  *  License version 2 as published by the Free Software Foundation;
00007  *
00008  *  This library is distributed in the hope that it will be useful,
00009  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011  *  Library General Public License for more details.
00012  *
00013  *  You should have received a copy of the GNU Library General Public License
00014  *  along with this library; see the file COPYING.LIB.  If not, write to
00015  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016  *  Boston, MA 02110-1301, USA.
00017  **/
00018 
00019 #include <sys/types.h>
00020 #include <sys/stat.h>
00021 #include <unistd.h>
00022 #include <stdlib.h>
00023 #include <stdio.h>
00024 
00025 #include <QtCore/QFile>
00026 #include <QtCore/QTextStream>
00027 #include <QtCore/QRegExp>
00028 #include <QtCore/Q_PID>
00029 
00030 #include <kcmdlineargs.h>
00031 #include <kapplication.h>
00032 #include <klocale.h>
00033 #include <kaboutdata.h>
00034 #include <kglobal.h>
00035 #include <kstandarddirs.h>
00036 #include <ktoolinvocation.h>
00037 #include <klauncher_iface.h>
00038 #include <kde_file.h>
00039 #include <QtDBus/QtDBus>
00040 
00041 static const char appName[] = "kdontchangethehostname";
00042 static const char appVersion[] = "1.1";
00043 
00044 class KHostName
00045 {
00046 public:
00047    KHostName();
00048 
00049    void changeX();
00050    void changeStdDirs(const QByteArray &type);
00051    void changeSessionManager();
00052 
00053 protected:
00054    QString oldName;
00055    QString newName;
00056    QString display;
00057    QByteArray home;
00058 };
00059 
00060 KHostName::KHostName()
00061 {
00062    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
00063    if (args->count() != 2)
00064       args->usage();
00065    oldName = args->arg(0);
00066    newName = args->arg(1);
00067    if (oldName == newName)
00068       exit(0);
00069 
00070    home = qgetenv("HOME");
00071    if (home.isEmpty())
00072    {
00073       fprintf(stderr, "%s", i18n("Error: HOME environment variable not set.\n").toLocal8Bit().data());
00074       exit(1);
00075    }
00076 
00077    display = QString::fromLocal8Bit(qgetenv("DISPLAY"));
00078    // strip the screen number from the display
00079    display.remove(QRegExp("\\.[0-9]+$"));
00080 #if defined(Q_WS_X11) || defined(Q_WS_QWS)
00081    if (display.isEmpty())
00082    {
00083       fprintf(stderr, "%s", i18n("Error: DISPLAY environment variable not set.\n").toLocal8Bit().data());
00084       exit(1);
00085    }
00086 #endif
00087 }
00088 
00089 static QList<QByteArray> split(const QByteArray &str)
00090 {
00091    const char *s = str.data();
00092    QList<QByteArray> result;
00093    while (*s)
00094    {
00095       const char *i = strchr(s, ' ');
00096       if (!i)
00097       {
00098          result.append(QByteArray(s));
00099          return result;
00100       }
00101       result.append(QByteArray(s, i-s+1));
00102       s = i;
00103       while (*s == ' ') s++;
00104    }
00105    return result;
00106 }
00107 
00108 void KHostName::changeX()
00109 {
00110    QProcess proc;
00111    proc.start("xauth", QStringList() << "-n" << "list");
00112    if (!proc.waitForFinished())
00113    {
00114       fprintf(stderr, "Warning: Can not run xauth.\n");
00115       return;
00116    }
00117    QList<QByteArray> lines;
00118    {
00119       while (!proc.atEnd())
00120       {
00121          QByteArray line = proc.readLine();
00122          if (line.length())
00123             line.truncate(line.length()-1); // Strip LF.
00124          if (!line.isEmpty())
00125             lines.append(line);
00126       }
00127    }
00128 
00129    foreach ( const QByteArray &it, lines )
00130    {
00131       QList<QByteArray> entries = split(it);
00132       if (entries.count() != 3)
00133          continue;
00134 
00135       QByteArray netId = entries[0];
00136       QByteArray authName = entries[1];
00137       QByteArray authKey = entries[2];
00138 
00139       int i = netId.lastIndexOf(':');
00140       if (i == -1)
00141          continue;
00142       QByteArray netDisplay = netId.mid(i);
00143       if (netDisplay != display)
00144          continue;
00145 
00146       i = netId.indexOf('/');
00147       if (i == -1)
00148          continue;
00149 
00150       QString newNetId = newName+netId.mid(i);
00151       QString oldNetId = netId.left(i);
00152 
00153       if (oldNetId != oldName)
00154     continue;
00155 
00156       QProcess::execute("xauth", QStringList() << "-n" << "remove" << netId);
00157       QProcess::execute("xauth", QStringList() << "-n" << "add" << newNetId << authName << authKey);
00158    }
00159 }
00160 
00161 void KHostName::changeStdDirs(const QByteArray &type)
00162 {
00163    // We make links to the old dirs cause we can't delete the old dirs.
00164    QByteArray oldDir = QFile::encodeName(QString("%1%2-%3").arg(KGlobal::dirs()->localkdedir()).arg(QString( type )).arg(QString( oldName )));
00165    QByteArray newDir = QFile::encodeName(QString("%1%2-%3").arg(KGlobal::dirs()->localkdedir()).arg(QString( type )).arg(QString( newName )));
00166 
00167    KDE_struct_stat st_buf;
00168 
00169    int result = KDE_lstat(oldDir.data(), &st_buf);
00170    if (result == 0)
00171    {
00172       if (S_ISLNK(st_buf.st_mode))
00173       {
00174          char buf[4096+1];
00175          result = readlink(oldDir.data(), buf, 4096);
00176          if (result >= 0)
00177          {
00178             buf[result] = 0;
00179             result = symlink(buf, newDir.data());
00180          }
00181       }
00182       else if (S_ISDIR(st_buf.st_mode))
00183       {
00184          result = symlink(oldDir.data(), newDir.data());
00185       }
00186       else
00187       {
00188          result = -1;
00189       }
00190    }
00191    if (result != 0)
00192    {
00193        const QString lnusertemp = KGlobal::dirs()->findExe( "lnusertemp" );
00194        QProcess::execute( lnusertemp, QStringList() << type );
00195    }
00196 }
00197 
00198 void KHostName::changeSessionManager()
00199 {
00200    QString sm = QString::fromLocal8Bit(qgetenv("SESSION_MANAGER"));
00201    if (sm.isEmpty())
00202    {
00203       fprintf(stderr, "Warning: No session management specified.\n");
00204       return;
00205    }
00206    int i = sm.lastIndexOf(':');
00207    if ((i == -1) || (sm.left(6) != "local/"))
00208    {
00209       fprintf(stderr, "Warning: Session Management socket '%s' has unexpected format.\n", sm.toLocal8Bit().constData());
00210       return;
00211    }
00212    sm = "local/"+newName+sm.mid(i);
00213    KToolInvocation::klauncher()->call(QDBus::NoBlock, "setLaunchEnv", QByteArray("SESSION_MANAGER"), sm);
00214 }
00215 
00216 int main(int argc, char **argv)
00217 {
00218    KAboutData d(appName, "kdelibs4", ki18n("KDontChangeTheHostName"), appVersion,
00219                 ki18n("Informs KDE about a change in hostname"),
00220                 KAboutData::License_GPL, ki18n("(c) 2001 Waldo Bastian"));
00221    d.addAuthor(ki18n("Waldo Bastian"), ki18n("Author"), "bastian@kde.org");
00222 
00223    KCmdLineOptions options;
00224    options.add("+old", ki18n("Old hostname"));
00225    options.add("+new", ki18n("New hostname"));
00226 
00227    KCmdLineArgs::init(argc, argv, &d);
00228    KCmdLineArgs::addCmdLineOptions(options);
00229 
00230    KComponentData k(&d);
00231 
00232    KHostName hn;
00233 
00234    hn.changeX();
00235    hn.changeStdDirs("socket");
00236    hn.changeStdDirs("tmp");
00237    hn.changeSessionManager();
00238 }
00239 

KDED

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal