kdeprint Library API Documentation

util.cpp

00001 /*
00002  *  This file is part of the KDE libraries
00003  *  Copyright (c) 2001 Michael Goffioul <kdeprint@swing.be>
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Library General Public
00007  *  License version 2 as published by the Free Software Foundation.
00008  *
00009  *  This library is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  *  Library General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU Library General Public License
00015  *  along with this library; see the file COPYING.LIB.  If not, write to
00016  *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017  *  Boston, MA 02111-1307, USA.
00018  **/
00019 
00020 #include "util.h"
00021 #include <qstringlist.h>
00022 
00023 KURL smbToUrl(const QString& work, const QString& server, const QString& printer)
00024 {
00025     KURL    url;
00026     url.setProtocol("smb");
00027     if (!work.isEmpty())
00028     {
00029         url.setHost(work);
00030         url.setPath("/" + server + "/" + printer);
00031     }
00032     else
00033     {
00034         url.setHost(server);
00035         url.setPath("/" + printer);
00036     }
00037     return url;
00038 }
00039 
00040 void urlToSmb(const KURL& url, QString& work, QString& server, QString& printer)
00041 {
00042     if (url.protocol() != "smb")
00043         return;
00044     QString h = url.host();
00045     QStringList l = QStringList::split('/', url.path(), false);
00046     if (l.count() > 1)
00047     {
00048         work = h;
00049         server = l[0];
00050         printer = l[1];
00051     }
00052     else
00053     {
00054         work = QString::null;
00055         server = h;
00056         printer = l[0];
00057     }
00058 }
00059 
00060 KURL smbToUrl(const QString& s)
00061 {
00062     // allow to handle non-encoded chars in login/password
00063     KURL    url;
00064     int p = s.find('@');
00065     if (p == -1)
00066     {
00067         // assumes url starts with "smb://". Use encoding in
00068         // case the printer name contains chars like '#'.
00069         url = KURL("smb://" + KURL::encode_string(s.mid(6)));
00070     }
00071     else
00072     {
00073         // assumes URL starts with "smb://"
00074         QString username = s.mid(6, p-6);
00075         url = KURL("smb://" + KURL::encode_string(s.mid(p+1)));
00076         int q = username.find(':');
00077         if (q == -1)
00078             url.setUser(username);
00079         else
00080         {
00081             url.setUser(username.left(q));
00082             url.setPass(username.mid(q+1));
00083         }
00084     }
00085     return url;
00086 }
00087 
00088 QString urlToSmb(const KURL& url)
00089 {
00090     // do not encode special chars in login/password
00091     QString s = "smb://";
00092     if (!url.user().isEmpty())
00093     {
00094         s.append(url.user());
00095         if (!url.pass().isEmpty())
00096             s.append(":").append(url.pass());
00097         s.append("@");
    }
    s.append(url.host()).append(KURL::decode_string(url.path()));
    return s;
}

int findIndex(int ID)
{
    for (int i=0; i<KPrinter::NPageSize-1; i++)
        if (page_sizes[i].ID == ID)
            return i;
    return 4;
}

QString buildSmbURI( const QString& work, const QString& server, const QString& printer, const QString& user, const QString& passwd )
{
    QString uri = server + "/" + printer;
00098     if ( !work.isEmpty() )
00099         uri.prepend( work + "/" );
00100     if ( !user.isEmpty() )
00101     {
00102         uri.prepend( "@" );
        if ( !passwd.isEmpty() )
            uri.prepend( ":" + passwd );
00103         uri.prepend( user );
00104     }
00105     uri.prepend( "smb://" );
00106     return uri;
00107 }
00108 
00109 bool splitSmbURI( const QString& uri, QString& work, QString& server, QString& printer, QString& user, QString& passwd )
00110 {
00111     int p( 0 );
00112     if ( !uri.startsWith( "smb://" ) )
00113         return false;
00114     p = 6;
00115 
00116     int p1 = uri.find( '/', p );
00117     if ( p1 != -1 )
00118     {
00119         int p2 = uri.find( '@', p );
00120         if ( p2 != -1 && p2 < p1 )
00121         {
00122             // Got a user
00123             int p3 = uri.find( ':', p );
00124             if ( p3 != -1 && p3 < p2 )
00125             {
00126                 // Got a password
00127                 user = uri.mid( p, p3-p );
00128                 passwd = uri.mid( p3+1, p2-p3-1 );
00129             }
00130             else
00131                 user = uri.mid( p, p2-p );
00132         }
00133         else
00134             p2 = p-1;
00135         QStringList l = QStringList::split( '/', uri.mid( p2+1 ), false );
00136         switch ( l.count() )
00137         {
00138             case 3:
00139                 work = l[ 0 ];
00140                 server = l[ 1 ];
00141                 printer = l[ 2 ];
00142                 break;
00143             case 2:
00144                 server = l[ 0 ];
00145                 printer = l[ 1 ];
00146                 break;
00147             default:
00148                 return false;
00149         }
00150         return true;
00151     }
00152     return false;
00153 }
00154 
00155 QString shadowPassword( const QString& uri )
00156 {
00157     QString result = uri;
00158     int p = result.find( ':' );
00159     if ( p != -1 )
00160     {
00161         while ( result[ ++p ] == '/' );
00162         int p1 = result.find( '@', p );
00163         if ( p1 != -1 )
00164         {
00165             int p2 = result.find( ':', p );
00166             if ( p2 != -1 && p2 < p1 )
00167             {
00168                 result.replace( p2, p1-p2, "" );
00169             }
00170         }
00171     }
00172     return result;
00173 }
00174 
KDE Logo
This file is part of the documentation for kdeprint Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Apr 21 18:44:37 2004 by doxygen 1.3.6-20040222 written by Dimitri van Heesch, © 1997-2003