summaryrefslogtreecommitdiffstats
path: root/src/gui/messageformview.cpp
blob: 31a073ca56fb7332ea7a8960ca51714737da1c5b (plain)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
    Copyright (C) 2005-2009  Michel de Boer <michel@twinklephone.com>

    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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "messageformview.h"

#include "gui.h"

#include "audits/memman.h"

MessageFormView::MessageFormView(QWidget *parent, im::t_msg_session *s) :
		MessageForm(parent),
		t_observer()
{
	_msgSession = s;
	_msgSession->attach(this);
	_destructing = false;

	connect(this, SIGNAL(update_signal()), this, SLOT(update_slot()));
}

MessageFormView::~MessageFormView()
{
	_destructing = true;
	if (_msgSession) {
		((t_gui *)ui)->removeMessageSession(_msgSession);
		MEMMAN_DELETE(_msgSession);
		delete _msgSession;
	}
}

void MessageFormView::updatePartyInfo(void)
{
	t_user *user_config = _msgSession->get_user();
	selectUserConfig(user_config);
	t_display_url to_url = _msgSession->get_remote_party();
	
	if (to_url.is_valid()) {
		toLineEdit->setText(ui->format_sip_address(user_config, to_url.display, to_url.url).c_str());
	} else {
		toLineEdit->clear();
	}
}

void MessageFormView::update_slot(void) {
	// Called directly from core, so lock GUI
	ui->lock();
	
	updatePartyInfo();
	setRemotePartyCaption();
	
	t_user *user_config = _msgSession->get_user();
	t_display_url to_url = _msgSession->get_remote_party();
	
	// Update msgLineEdit field based on msg-in-flight indication
	if (!_msgSession->is_msg_in_flight() && !msgLineEdit->isEnabled()) {
		msgLineEdit->clear();
		
		// When the user edits the message, the composition indication
		// will be set to active.
		connect(msgLineEdit, SIGNAL(textChanged(const QString &)),		
			this, SLOT(setLocalComposingIndicationActive()));
		
		// Enable msgLineEdit first, otherwise the setFocus does not work
		msgLineEdit->setEnabled(true);
		
		msgLineEdit->setFocus();
	} else if (_msgSession->is_msg_in_flight() && msgLineEdit->isEnabled()) {
		// Disable the triggering of the composition indication while a message
		// is being sent.
		disconnect(msgLineEdit, SIGNAL(textChanged(const QString &)),
			   this, SLOT(setLocalComposingIndicationActive()));
		msgLineEdit->setText(tr("sending message"));
	}
	
	// Enable/disable msgLineEdit here to be robust, such that msgLineEdit
	// does not stay disabled forever.
	msgLineEdit->setEnabled(!_msgSession->is_msg_in_flight());
	
	sendFileAction->setEnabled(!_msgSession->is_msg_in_flight());
	
	// Display error
	if (_msgSession->error_received()) {
		string error_msg = _msgSession->take_error();
		displayError(error_msg.c_str());
	}
	
	// Display delivery notification
	if (_msgSession->delivery_notification_received()) {
		string notification = _msgSession->take_delivery_notification();
		displayDeliveryNotification(notification.c_str());
	}
	
	// Display message composing indication
	if (_msgSession->get_remote_composing_state() == im::COMPOSING_STATE_ACTIVE) {
		QString name = to_url.display.c_str();
		if (name.isEmpty()) {
			name = to_url.url.get_user().c_str();
		}
		
		setComposingIndication(name);
	} else {
		clearComposingIndication();
	}
	
	// Display message
	if (_msgSession->is_new_message_added()) {
		im::t_msg m;
		try {
			m = _msgSession->get_last_message();
		} catch (empty_list_exception) {
			ui->unlock();
			return;
		}
		
		QString name;
		if (m.direction == im::MSG_DIR_IN) {
			name = to_url.display.c_str();
			if (name.isEmpty()) {
				name = to_url.url.get_user().c_str();
			}
		} else {
			name = user_config->get_display(false).c_str();
			if (name.isEmpty()) {
				name = user_config->get_name().c_str();
			}
		}
		
		addMessage(m, name);
	}
	
	ui->unlock();
}

void MessageFormView::update(void) {
	emit update_signal();
}

void MessageFormView::subject_destroyed()
{
	_msgSession = NULL;
	
	if (!_destructing) close();
}

void MessageFormView::show()
{
	((t_gui *)ui)->fill_user_combo(fromComboBox);
	updatePartyInfo();
	MessageForm::show();
}