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., 51 Franklin Street, Fifth Floor,
00017  *  Boston, MA 02110-1301, USA.
00018  **/
00019 
00020 #include "util.h"
00021 #include <qstringlist.h>
00022 
00023 void urlToSmb(const KURL& url, QString& work, QString& server, QString& printer)
00024 {
00025     if (url.protocol() != "smb")
00026         return;
00027     QString h = url.host();
00028     QStringList l = QStringList::split('/', url.path(), false);
00029     if (l.count() > 1)
00030     {
00031         work = h;
00032         server = l[0];
00033         printer = l[1];
00034     }
00035     else
00036     {
00037         work = QString::null;
00038         server = h;
00039         printer = l[0];
00040     }
00041 }
00042 
00043 KURL smbToUrl(const QString& s)
00044 {
00045     // allow to handle non-encoded chars in login/password
00046     KURL    url;
00047     int p = s.find('@');
00048     if (p == -1)
00049     {
00050         // assumes url starts with "smb://". Use encoding in
00051         // case the printer name contains chars like '#'.
00052         url = KURL("smb://" + KURL::encode_string(s.mid(6)));
00053     }
00054     else
00055     {
00056         // assumes URL starts with "smb://"
00057         QString username = s.mid(6, p-6);
00058         url = KURL("smb://" + KURL::encode_string(s.mid(p+1)));
00059         int q = username.find(':');
00060         if (q == -1)
00061             url.setUser(username);
00062         else
00063         {
00064             url.setUser(username.left(q));
00065             url.setPass(username.mid(q+1));
00066         }
00067     }
00068     return url;
00069 }
00070 
00071 int findIndex(int ID)
00072 {
00073     for (int i=0; i<KPrinter::NPageSize-1; i++)
00074         if (page_sizes[i].ID == ID)
00075             return i;
00076     return 4;
00077 }
00078 
00079 QString buildSmbURI( const QString& work, const QString& server, const QString& printer, const QString& user, const QString& passwd )
00080 {
00081     QString uri = server + "/" + printer;
00082     if ( !work.isEmpty() )
00083         uri.prepend( work + "/" );
00084     if ( !user.isEmpty() )
00085     {
00086         uri.prepend( "@" );
00087         if ( !passwd.isEmpty() )
00088             uri.prepend( ":" + passwd );
00089         uri.prepend( user );
00090     }
00091     uri.prepend( "smb://" );
00092     return uri;
00093 }
00094 
00095 bool splitSmbURI( const QString& uri, QString& work, QString& server, QString& printer, QString& user, QString& passwd )
00096 {
00097     int p( 0 );
00098     if ( !uri.startsWith( "smb://" ) )
00099         return false;
00100     p = 6;
00101 
00102     int p1 = uri.find( '/', p );
00103     if ( p1 != -1 )
00104     {
00105         int p2 = uri.find( '@', p );
00106         if ( p2 != -1 && p2 < p1 )
00107         {
00108             // Got a user
00109             int p3 = uri.find( ':', p );
00110             if ( p3 != -1 && p3 < p2 )
00111             {
00112                 // Got a password
00113                 user = uri.mid( p, p3-p );
00114                 passwd = uri.mid( p3+1, p2-p3-1 );
00115             }
00116             else
00117                 user = uri.mid( p, p2-p );
00118         }
00119         else
00120             p2 = p-1;
00121         QStringList l = QStringList::split( '/', uri.mid( p2+1 ), false );
00122         switch ( l.count() )
00123         {
00124             case 3:
00125                 work = l[ 0 ];
00126                 server = l[ 1 ];
00127                 printer = l[ 2 ];
00128                 break;
00129             case 2:
00130                 server = l[ 0 ];
00131                 printer = l[ 1 ];
00132                 break;
00133             default:
00134                 return false;
00135         }
00136         return true;
00137     }
00138     return false;
00139 }
KDE Home | KDE Accessibility Home | Description of Access Keys