• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdegraphics API Reference
  • KDE Home
  • Contact Us
 

libs/libksane/libksane

  • sources
  • kde-4.14
  • kdegraphics
  • libs
  • libksane
  • libksane
ksane_auth.cpp
Go to the documentation of this file.
1 /* ============================================================
2 *
3 * This file is part of the KDE project
4 *
5 * Date : 2010
6 * Description : Sane authentication helpers.
7 *
8 * Copyright (C) 2010 by Kare Sars <kare dot sars at iki dot fi>
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) version 3, or any
14 * later version accepted by the membership of KDE e.V. (or its
15 * successor approved by the membership of KDE e.V.), which shall
16 * act as a proxy defined in Section 6 of version 3 of the license.
17 *
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
22 *
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this program. If not, see <http://www.gnu.org/licenses/>.
25 *
26 * ============================================================ */
27 
28 #include "ksane_auth.h"
29 
30 // Qt includes
31 #include <QMutex>
32 #include <QList>
33 
34 // KDE includes
35 #include <KDebug>
36 
37 namespace KSaneIface
38 {
39 
40 static KSaneAuth *s_instance = 0;
41 static QMutex s_mutex;
42 
43 struct KSaneAuth::Private
44 {
45  struct AuthStruct
46  {
47  QString resource;
48  QString username;
49  QString password;
50  };
51 
52  QList<AuthStruct> authList;
53 };
54 
55 KSaneAuth *KSaneAuth::getInstance()
56 {
57  s_mutex.lock();
58 
59  if (s_instance == 0) {
60  s_instance = new KSaneAuth();
61  }
62  s_mutex.unlock();
63 
64  return s_instance;
65 }
66 
67 KSaneAuth::KSaneAuth() : d(new Private) {}
68 
69 KSaneAuth::~KSaneAuth()
70 {
71  d->authList.clear();
72  delete d;
73 }
74 
75 void KSaneAuth::setDeviceAuth(const QString &resource, const QString &username, const QString &password)
76 {
77  // This is a short list so we do not need a QMap...
78  int i;
79  for (i=0; i<d->authList.size(); i++) {
80  if (resource == d->authList.at(i).resource) {
81  // update the existing node
82  d->authList[i].username = username;
83  d->authList[i].password = password;
84  break;
85  }
86  }
87  if (i==d->authList.size()) {
88  // Add a new list node
89  Private::AuthStruct tmp;
90  tmp.resource = resource;
91  tmp.username = username;
92  tmp.password = password;
93  d->authList << tmp;
94  }
95 }
96 
97 void KSaneAuth::clearDeviceAuth(const QString &resource)
98 {
99  // This is a short list so we do not need a QMap...
100  for (int i=0; i<d->authList.size(); i++) {
101  if (resource == d->authList.at(i).resource) {
102  d->authList.removeAt(i);
103  return;
104  }
105  }
106 }
107 
109 void KSaneAuth::authorization(SANE_String_Const resource, SANE_Char *username, SANE_Char *password)
110 {
111  kDebug() << resource;
112  // This is vague in the standard... what can I find in the resource string?
113  // I have found that "resource contains the backend name + "$MD5$....."
114  // it does not contain unique identifiers like ":libusb:001:004"
115  // -> remove $MD5 and later before comparison...
116  QString res(resource);
117  int end = res.indexOf("$MD5$");
118  res = res.left(end);
119  kDebug() << res;
120 
121  QList<Private::AuthStruct> list = getInstance()->d->authList;
122  for (int i=0; i<list.size(); i++) {
123  kDebug() << res << list.at(i).resource;
124  if (list.at(i).resource.contains(res)) {
125  qstrncpy(username, list.at(i).username.toLocal8Bit(), SANE_MAX_USERNAME_LEN);
126  qstrncpy(password, list.at(i).password.toLocal8Bit(), SANE_MAX_PASSWORD_LEN);
127  break;
128  }
129  }
130 }
131 
132 
133 
134 
135 }
136 
QString::indexOf
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
QMutex
KSaneIface::KSaneAuth::clearDeviceAuth
void clearDeviceAuth(const QString &resource)
Definition: ksane_auth.cpp:97
QList::at
const T & at(int i) const
KSaneIface::s_mutex
static QMutex s_mutex
Definition: ksane_auth.cpp:41
KSaneIface::s_instance
static KSaneAuth * s_instance
Definition: ksane_auth.cpp:40
KSaneIface::KSaneAuth
Definition: ksane_auth.h:44
QMutex::unlock
void unlock()
QList::size
int size() const
KSaneIface::KSaneAuth::setDeviceAuth
void setDeviceAuth(const QString &resource, const QString &username, const QString &password)
Definition: ksane_auth.cpp:75
KSaneIface::KSaneAuth::getInstance
static KSaneAuth * getInstance()
Definition: ksane_auth.cpp:55
KSaneIface::KSaneAuth::authorization
static void authorization(SANE_String_Const resource, SANE_Char *username, SANE_Char *password)
static function called by sane_open to get authorization from user
Definition: ksane_auth.cpp:109
ksane_auth.h
KSaneIface::KSaneAuth::~KSaneAuth
~KSaneAuth()
Definition: ksane_auth.cpp:69
QString
QList< AuthStruct >
QMutex::lock
void lock()
QString::left
QString left(int n) const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:19:47 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

libs/libksane/libksane

Skip menu "libs/libksane/libksane"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdegraphics API Reference

Skip menu "kdegraphics API Reference"
  •     libkdcraw
  •     libkexiv2
  •     libkipi
  •     libksane
  • okular

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal