1 /*
2     SPDX-License-Identifier: GPL-2.0-or-later
3 
4     SPDX-FileCopyrightText: 2003 Dario Abatianni <eisfuchs@tigress.com>
5 */
6 
7 #include "identity.h"
8 #include "irccharsets.h"
9 
10 #include <QTextCodec>
11 
12 
13 int Identity::s_availableId = 0;
14 
Identity()15 Identity::Identity() : QSharedData()
16 {
17     m_id = s_availableId;
18     s_availableId++;
19 
20     init();
21 }
22 
Identity(int id)23 Identity::Identity(int id) : QSharedData()
24 {
25     if (id < 0)
26     {
27         m_id = s_availableId;
28         s_availableId++;
29     }
30     else
31     {
32         m_id = id;
33     }
34 
35     init();
36 }
37 
Identity(const Identity & original)38 Identity::Identity(const Identity& original) : QSharedData()
39 {
40     copy(original);
41     m_id = original.id();
42 }
43 
~Identity()44 Identity::~Identity()
45 {
46 }
47 
init()48 void Identity::init()
49 {
50     setAuthType(QStringLiteral("saslplain"));
51     setNickservNickname(QStringLiteral("nickserv"));
52     setNickservCommand(QStringLiteral("identify"));
53 
54     setCodecName(Konversation::IRCCharsets::self()->encodingForLocale());
55 
56     setInsertRememberLineOnAway(false);
57 
58     setQuitReason(QStringLiteral("Konversation terminated!"));
59     setPartReason(QStringLiteral("Konversation terminated!"));
60     setKickReason(QStringLiteral("User terminated!"));
61 
62     setAwayMessage(QStringLiteral("Gone away for now"));
63 
64     setRunAwayCommands(false);
65 
66     setAutomaticAway(false);
67     setAwayInactivity(10);
68     setAutomaticUnaway(false);
69 }
70 
copy(const Identity & original)71 void Identity::copy(const Identity& original)
72 {
73     setName(original.getName());
74     setRealName(original.getRealName());
75     setIdent(original.getIdent());
76     setNicknameList(original.getNicknameList());
77     setAuthType(original.getAuthType());
78     setAuthPassword(original.getAuthPassword());
79     setNickservNickname(original.getNickservNickname());
80     setNickservCommand(original.getNickservCommand());
81     setSaslAccount(original.getSaslAccount());
82     setPemClientCertFile(original.getPemClientCertFile());
83     setQuitReason(original.getQuitReason());
84     setPartReason(original.getPartReason());
85     setKickReason(original.getKickReason());
86     setInsertRememberLineOnAway(original.getInsertRememberLineOnAway());
87     setRunAwayCommands(original.getRunAwayCommands());
88     setAwayCommand(original.getAwayCommand());
89     setAwayMessage(original.getAwayMessage());
90     setAwayNickname(original.getAwayNickname());
91     setReturnCommand(original.getReturnCommand());
92     setAutomaticAway(original.getAutomaticAway());
93     setAwayInactivity(original.getAwayInactivity());
94     setAutomaticUnaway(original.getAutomaticUnaway());
95     setShellCommand(original.getShellCommand());
96     setCodecName(original.getCodecName());
97 }
98 
setName(const QString & newName)99 void Identity::setName(const QString& newName)          { name=newName; }
getName() const100 QString Identity::getName() const                       { return name; }
101 
setRealName(const QString & name)102 void Identity::setRealName(const QString& name)         { realName=name; }
getRealName() const103 QString Identity::getRealName() const                   { return realName; }
setIdent(const QString & newIdent)104 void Identity::setIdent(const QString& newIdent)        { ident=newIdent; }
getIdent() const105 QString Identity::getIdent() const                      { return ident; }
106 
setNickname(uint index,const QString & newName)107 void Identity::setNickname(uint index,const QString& newName) { nicknameList[index]=newName; }
108 
getNickname(int index) const109 QString Identity::getNickname(int index) const
110 {
111   return nicknameList.value(index);
112 }
113 
setAuthType(const QString & authType)114 void Identity::setAuthType(const QString& authType)     { m_authType = authType; }
getAuthType() const115 QString Identity::getAuthType() const                   { return m_authType; }
setAuthPassword(const QString & authPassword)116 void Identity::setAuthPassword(const QString& authPassword) { m_authPassword = authPassword; }
getAuthPassword() const117 QString Identity::getAuthPassword() const                   { return m_authPassword; }
setNickservNickname(const QString & nickservNickname)118 void Identity::setNickservNickname(const QString& nickservNickname) { m_nickservNickname = nickservNickname; }
getNickservNickname() const119 QString Identity::getNickservNickname() const                       { return m_nickservNickname; }
setNickservCommand(const QString & nickservCommand)120 void Identity::setNickservCommand(const QString& nickservCommand) { m_nickservCommand = nickservCommand; }
getNickservCommand() const121 QString Identity::getNickservCommand() const                      { return m_nickservCommand; }
setSaslAccount(const QString & saslAccount)122 void Identity::setSaslAccount(const QString& saslAccount) { m_saslAccount = saslAccount; }
getSaslAccount() const123 QString Identity::getSaslAccount() const                  { return m_saslAccount; }
setPemClientCertFile(const QUrl & url)124 void Identity::setPemClientCertFile(const QUrl &url)      { m_pemClientCertFile = url; }
getPemClientCertFile() const125 QUrl Identity::getPemClientCertFile() const               { return m_pemClientCertFile; }
126 
setQuitReason(const QString & reason)127 void Identity::setQuitReason(const QString& reason)     { quitReason=reason; }
getQuitReason() const128 QString Identity::getQuitReason() const                 { return quitReason; }
setPartReason(const QString & reason)129 void Identity::setPartReason(const QString& reason)     { partReason=reason; }
getPartReason() const130 QString Identity::getPartReason() const                 { return partReason; }
setKickReason(const QString & reason)131 void Identity::setKickReason(const QString& reason)     { kickReason=reason; }
getKickReason() const132 QString Identity::getKickReason() const                 { return kickReason; }
133 
setInsertRememberLineOnAway(bool state)134 void Identity::setInsertRememberLineOnAway(bool state) { insertRememberLineOnAway = state; }
getInsertRememberLineOnAway() const135 bool Identity::getInsertRememberLineOnAway() const { return insertRememberLineOnAway; }
136 
setRunAwayCommands(bool run)137 void Identity::setRunAwayCommands(bool run)             { runAwayCommands = run; }
getRunAwayCommands() const138 bool Identity::getRunAwayCommands() const               { return runAwayCommands; }
setAwayCommand(const QString & command)139 void Identity::setAwayCommand(const QString& command)   { awayCommand = command; }
getAwayCommand() const140 QString Identity::getAwayCommand() const                { return awayCommand; }
setReturnCommand(const QString & command)141 void Identity::setReturnCommand(const QString& command) { returnCommand = command; }
getReturnCommand() const142 QString Identity::getReturnCommand() const              { return returnCommand; }
143 
setAutomaticAway(bool automaticAway)144 void Identity::setAutomaticAway(bool automaticAway)     { m_automaticAway = automaticAway; }
getAutomaticAway() const145 bool Identity::getAutomaticAway() const                 { return m_automaticAway; }
setAwayInactivity(int awayInactivity)146 void Identity::setAwayInactivity(int awayInactivity)    { m_awayInactivity = awayInactivity; }
getAwayInactivity() const147 int Identity::getAwayInactivity() const                 { return m_awayInactivity; }
setAutomaticUnaway(bool automaticUnaway)148 void Identity::setAutomaticUnaway(bool automaticUnaway) { m_automaticUnaway = automaticUnaway; }
getAutomaticUnaway() const149 bool Identity::getAutomaticUnaway() const               { return m_automaticUnaway; }
150 
setNicknameList(const QStringList & newList)151 void Identity::setNicknameList(const QStringList& newList)
152 {
153     nicknameList.clear();
154     nicknameList = newList;
155 }
156 
getNicknameList() const157 QStringList Identity::getNicknameList() const           { return nicknameList; }
158 
getShellCommand() const159 QString Identity::getShellCommand() const { return m_shellCommand;}
setShellCommand(const QString & command)160 void Identity::setShellCommand(const QString& command) { m_shellCommand=command;}
161 
getCodec() const162 QTextCodec* Identity::getCodec() const                  { return m_codec; }
getCodecName() const163 QString Identity::getCodecName() const                  { return m_codecName; }
setCodecName(const QString & newCodecName)164 void Identity::setCodecName(const QString &newCodecName)
165 {
166     // NOTE: codecName should be based on KCharsets::availableEncodingNames() / descriptiveEncodingNames()
167     // We can get a QTextCodec from QString based on them, but can't do the reverse of that.
168 
169     // never set an empty or borked codec!
170     QString codecName = newCodecName;
171     if (!Konversation::IRCCharsets::self()->isValidEncoding(codecName))
172         codecName = Konversation::IRCCharsets::self()->encodingForLocale();
173 
174     m_codecName = codecName;
175     m_codec = Konversation::IRCCharsets::self()->codecForName(codecName);
176 
177     if (!m_codec) {
178         setCodecName(QStringLiteral("UTF-8"));
179     }
180 }
181 
setAwayMessage(const QString & message)182 void Identity::setAwayMessage(const QString& message)   { awayMessage = message; }
getAwayMessage() const183 QString Identity::getAwayMessage() const                { return awayMessage; }
184 
setAwayNickname(const QString & nickname)185 void Identity::setAwayNickname(const QString& nickname) { awayNickname = nickname; }
getAwayNickname() const186 QString Identity::getAwayNickname() const               { return awayNickname; }
187