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("@");
00098     }
00099     s.append(url.host()).append(KURL::decode_string(url.path()));
00100     return s;
00101 }
00102 
00103 int findIndex(int ID)
00104 {
00105     for (int i=0; i<KPrinter::NPageSize-1; i++)
00106         if (page_sizes[i].ID == ID)
00107             return i;
00108     return 4;
00109 }
00110 
00111 QString buildSmbURI( const QString& work, const QString& server, const QString& printer, const QString& user, const QString& passwd )
00112 {
00113     QString uri = server + "/" + printer;
00114     if ( !work.isEmpty() )
00115         uri.prepend( work + "/" );
00116     if ( !user.isEmpty() )
00117     {
00118         uri.prepend( "@" );
00119         if ( !passwd.isEmpty() )
00120             uri.prepend( ":" + passwd );
00121         uri.prepend( user );
00122     }
00123     uri.prepend( "smb://" );
00124     return uri;
00125 }
00126 
00127 bool splitSmbURI( const QString& uri, QString& work, QString& server, QString& printer, QString& user, QString& passwd )
00128 {
00129     int p( 0 );
00130     if ( !uri.startsWith( "smb://" ) )
00131         return false;
00132     p = 6;
00133 
00134     int p1 = uri.find( '/', p );
00135     if ( p1 != -1 )
00136     {
00137         int p2 = uri.find( '@', p );
00138         if ( p2 != -1 && p2 < p1 )
00139         {
00140             // Got a user
00141             int p3 = uri.find( ':', p );
00142             if ( p3 != -1 && p3 < p2 )
00143             {
00144                 // Got a password
00145                 user = uri.mid( p, p3-p );
00146                 passwd = uri.mid( p3+1, p2-p3-1 );
00147             }
00148             else
00149                 user = uri.mid( p, p2-p );
00150         }
00151         else
00152             p2 = p-1;
00153         QStringList l = QStringList::split( '/', uri.mid( p2+1 ), false );
00154         switch ( l.count() )
00155         {
00156             case 3:
00157                 work = l[ 0 ];
00158                 server = l[ 1 ];
00159                 printer = l[ 2 ];
00160                 break;
00161             case 2:
00162                 server = l[ 0 ];
00163                 printer = l[ 1 ];
00164                 break;
00165             default:
00166                 return false;
00167         }
00168         return true;
00169     }
00170     return false;
00171 }
00172 
00173 QString shadowPassword( const QString& uri )
00174 {
00175     QString result = uri;
00176     int p = result.find( ':' );
00177     if ( p != -1 )
00178     {
00179         while ( result[ ++p ] == '/' );
00180         int p1 = result.find( '@', p );
00181         if ( p1 != -1 )
00182         {
00183             int p2 = result.find( ':', p );
00184             if ( p2 != -1 && p2 < p1 )
00185             {
00186                 result.replace( p2, p1-p2, "" );
00187             }
00188         }
00189     }
00190     return result;
00191 }
KDE Logo
This file is part of the documentation for kdeprint Library Version 3.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Jul 20 12:45:45 2006 by doxygen 1.4.4 written by Dimitri van Heesch, © 1997-2003