~ubuntu-branches/ubuntu/wily/sflphone/wily

« back to all changes in this revision

Viewing changes to kde/src/lib/uri.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2015-01-07 14:51:16 UTC
  • mfrom: (4.3.5 sid)
  • Revision ID: package-import@ubuntu.com-20150107145116-yxnafinf4lrdvrmx
Tags: 1.4.1-0.1ubuntu1
* Merge with Debian, remaining changes:
 - Drop soprano, nepomuk build-dep
* Drop ubuntu patches, now upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
 *   Copyright (C) 2014 by Savoir-Faire Linux                               *
 
3
 *   Author : Emmanuel Lepage Vallee <emmanuel.lepage@savoirfairelinux.com> *
 
4
 *                                                                          *
 
5
 *   This library is free software; you can redistribute it and/or          *
 
6
 *   modify it under the terms of the GNU Lesser General Public             *
 
7
 *   License as published by the Free Software Foundation; either           *
 
8
 *   version 2.1 of the License, or (at your option) any later version.     *
 
9
 *                                                                          *
 
10
 *   This library is distributed in the hope that it will be useful,        *
 
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU      *
 
13
 *   Lesser General Public License for more details.                        *
 
14
 *                                                                          *
 
15
 *   You should have received a copy of the GNU General Public License      *
 
16
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.  *
 
17
 ***************************************************************************/
 
18
#include "uri.h"
 
19
 
 
20
constexpr const char* URI::schemeNames[];
 
21
 
 
22
URI::URI(const QString& other):QString()
 
23
   ,m_Parsed(false),m_HeaderType(SchemeType::NONE)
 
24
{
 
25
   m_Stripped                     = strip(other,m_HeaderType);
 
26
   (*static_cast<QString*>(this)) = m_Stripped               ;
 
27
}
 
28
 
 
29
URI::URI(const URI& o):QString(),m_Parsed(o.m_Parsed),m_Hostname(o.m_Hostname),
 
30
   m_HeaderType(o.m_HeaderType),m_Userinfo(o.m_Userinfo),m_Stripped(o.m_Stripped)
 
31
{
 
32
   (*static_cast<QString*>(this)) = o.m_Stripped;
 
33
}
 
34
 
 
35
///Strip out <sip:****> from the URI
 
36
QString URI::strip(const QString& uri, SchemeType& sheme)
 
37
{
 
38
   if (uri.isEmpty())
 
39
      return QString();
 
40
   int start(0),end(uri.size()-1); //Other type of comparisons were too slow
 
41
   if (end > 5 && uri[0] == '<' ) {
 
42
      if (uri[4] == ':') {
 
43
         sheme = uri[1] == 's'?SchemeType::SIP:SchemeType::IAX;
 
44
         start = 5;
 
45
      }
 
46
      else if (uri[5] == ':') {
 
47
         sheme = SchemeType::SIPS;
 
48
         start = 6;
 
49
      }
 
50
   }
 
51
   if (end && uri[end] == '>')
 
52
      end--;
 
53
   else if (start) {
 
54
      //TODO there may be a ';' section with arguments, check
 
55
   }
 
56
   return uri.mid(start,end-start+1);
 
57
}
 
58
 
 
59
///Return the domaine of an URI (<sip:12345@example.com>)
 
60
QString URI::hostname() const
 
61
{
 
62
   if (!m_Parsed)
 
63
      const_cast<URI*>(this)->parse();
 
64
   return m_Hostname;
 
65
}
 
66
 
 
67
bool URI::hasHostname() const
 
68
{
 
69
   if (!m_Parsed)
 
70
      const_cast<URI*>(this)->parse();
 
71
   return !m_Hostname.isEmpty();
 
72
}
 
73
 
 
74
///Keep a cache of the values to avoid re-parsing them
 
75
void URI::parse()
 
76
{
 
77
   if (indexOf('@') != -1) {
 
78
      const QStringList splitted = split('@');
 
79
      m_Hostname = splitted[1];//splitted[1].left(splitted[1].size())
 
80
      m_Userinfo = splitted[0];
 
81
      m_Parsed = true;
 
82
   }
 
83
}
 
84
 
 
85
QString URI::userinfo() const
 
86
{
 
87
   if (!m_Parsed)
 
88
      const_cast<URI*>(this)->parse();
 
89
   return m_Userinfo;
 
90
}
 
91
 
 
92
/**
 
93
 * Some feature, like SIP presence, require a properly formatted USI
 
94
 */
 
95
QString URI::fullUri() const
 
96
{
 
97
   return QString("<%1%2>")
 
98
      .arg(schemeNames[static_cast<int>(m_HeaderType == SchemeType::NONE?SchemeType::SIP:m_HeaderType)])
 
99
      .arg(*this);
 
100
}
 
 
b'\\ No newline at end of file'