aboutsummaryrefslogtreecommitdiffstats
path: root/src/modules/spaste/SlowPasteController.cpp
blob: f60e84396d1c59c50278e2bf0c21117b4f89bfea (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//=============================================================================
//
//   File : SlowPasteController.cpp
//   Creation date : Thu Apr 30 2002 17:13:12 GMT by Juanjo Álvarez
//
//   This file is part of the KVIrc IRC client distribution
//   Copyright (C) 2002 Juanjo Álvarez (juanjux@yahoo.es)
//   Copyright (C) 2002-2010 Szymon Stefanek (kvirc@tin.it)
//
//   This program is FREE software. You can redistribute it and/or
//   modify it under the terms of the GNU General Public License
//   as published by the Free Software Foundation; either version 2
//   of the License, or (at your option) any later version.
//
//   This program is distributed in the HOPE that it will be USEFUL,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//   See the GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program. If not, write to the Free Software Foundation,
//   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//=============================================================================

#include "SlowPasteController.h"

#include "KviWindow.h"
#include "KviConsoleWindow.h"
#include "KviControlCodes.h"
#include "KviApplication.h"
#include "KviOptions.h"

#include <QTimer>
#include <QString>
#include <QStringList>
#include <QClipboard>

extern KviPointerList<SlowPasteController> * g_pControllerList;

SlowPasteController::SlowPasteController(KviWindow * w, int id)
    : m_pClipBuff(nullptr), m_pFile(nullptr), m_pId(id), m_pWindow(w)
{
	g_pControllerList->append(this);
	//m_pWindow = w;
	m_pTimer = new QTimer(this);
}

SlowPasteController::~SlowPasteController()
{
	g_pControllerList->removeRef(this);
	if(m_pFile)
	{
		m_pFile->close();
		delete m_pFile;
	}
	if(m_pTimer)
	{
		m_pTimer->stop();
		delete m_pTimer;
	}
	if(m_pClipBuff)
		delete m_pClipBuff;
}

bool SlowPasteController::pasteFileInit(QString & fileName)
{
	if(m_pClipBuff)
		return false; // can't paste a file while pasting the clipboard
	if(m_pFile)
		return false; // can't paste two files at a time
	m_pFile = new QFile(fileName);
	if(!m_pFile->open(QIODevice::ReadOnly))
		return false;
	//avoid double connection
	disconnect(m_pTimer, SIGNAL(timeout()), nullptr, nullptr);
	connect(m_pTimer, SIGNAL(timeout()), this, SLOT(pasteFile()));
	//avoid timer reset if always active
	if(!m_pTimer->isActive())
		m_pTimer->start(KVI_OPTION_UINT(KviOption_uintPasteDelay));
	return true;
}

bool SlowPasteController::pasteClipboardInit()
{
	if(m_pFile)
		return false; // can't paste clipboard while pasting a file
	QString tmp(g_pApp->clipboard()->text());
	if(m_pClipBuff)
	{
		(*m_pClipBuff) += tmp.isEmpty() ? QStringList() : tmp.split("\n", Qt::KeepEmptyParts);
	}
	else
	{
		m_pClipBuff = new QStringList(tmp.isEmpty() ? QStringList() : tmp.split("\n", Qt::KeepEmptyParts));
	}
	//avoid double connection
	disconnect(m_pTimer, SIGNAL(timeout()), nullptr, nullptr);
	connect(m_pTimer, SIGNAL(timeout()), this, SLOT(pasteClipboard()));
	//avoid timer reset if always active
	if(!m_pTimer->isActive())
		m_pTimer->start(KVI_OPTION_UINT(KviOption_uintPasteDelay));
	return true;
}

void SlowPasteController::pasteFile()
{
	QString line;
	char data[1024];
	if(m_pFile->readLine(data, sizeof(data)) != -1)
	{
		line = data;
		if(line.isEmpty())
			line = QChar(KviControlCodes::Reset);

		line.replace('\t', QString(KVI_OPTION_UINT(KviOption_uintSpacesToExpandTabulationInput), ' ')); //expand tabs to spaces

		if(!g_pApp->windowExists(m_pWindow))
		{
			m_pFile->close();
			delete this;
		}
		else
		{
			m_pWindow->ownMessage(line.toLatin1());
		}
	}
	else
	{ //File finished
		m_pFile->close();
		delete this;
	}
}

void SlowPasteController::pasteClipboard()
{
	if(m_pClipBuff->isEmpty() || !g_pApp->windowExists(m_pWindow))
	{
		delete this;
	}
	else
	{
		QString line = m_pClipBuff->takeFirst();
		line.replace('\t', QString(KVI_OPTION_UINT(KviOption_uintSpacesToExpandTabulationInput), ' ')); //expand tabs to spaces
		m_pWindow->ownMessage(line);
	}
}