summaryrefslogtreecommitdiffstats
path: root/src/main-cmdlineact.cpp
blob: 2d0a5cfd607d2c70c3e3cd711cc0370d067edd07 (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
/*
 * Authors:
 *   Ted Gould <ted@gould.cx>
 *
 * Copyright (C) 2007 Authors
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information
 */

#include <ui/view/view.h>
#include <desktop.h>
#include <helper/action.h>
#include <selection.h>
#include <verbs.h>
#include <inkscape.h>
#include <document.h>
#include <file.h>

#include <glibmm/i18n.h>

#include "main-cmdlineact.h"

namespace Inkscape {

std::list <CmdLineAction *> CmdLineAction::_list;
bool CmdLineAction::_requestQuit = false;

CmdLineAction::CmdLineAction (bool isVerb, gchar const * arg) : _isVerb(isVerb), _arg(NULL) {
	if (arg != NULL) {
		_arg = g_strdup(arg);
	}

	_list.insert(_list.end(), this);

	return;
}

CmdLineAction::~CmdLineAction () {
	if (_arg != NULL) {
		g_free(_arg);
	}
}

bool
CmdLineAction::isExtended() {
	return false;
}

void
CmdLineAction::doItX (ActionContext const & context)
{
	(void)context;
	printf("CmdLineAction::doItX() %s\n", _arg);
}

void
CmdLineAction::doIt (ActionContext const & context) {
	//printf("Doing: %s\n", _arg);
	if (_isVerb) {
		if (isExtended()) {
			//printf("Is extended\n");

			doItX(context);
			return;
		}

		static std::string quit_verb_name = "FileQuit";
		if (quit_verb_name == _arg) {
			_requestQuit = true;
			return;
		}
		Inkscape::Verb * verb = Inkscape::Verb::getbyid(_arg);
		if (verb == NULL) {
			printf(_("Unable to find verb ID '%s' specified on the command line.\n"), _arg);
			return;
		}
		SPAction * action = verb->get_action(context);
		sp_action_perform(action, NULL);
	} else {
		if (context.getDocument() == NULL || context.getSelection() == NULL) { return; }

		SPDocument * doc = context.getDocument();
		SPObject * obj = doc->getObjectById(_arg);
		if (obj == NULL) {
			printf(_("Unable to find node ID: '%s'\n"), _arg);
			return;
		}

		Inkscape::Selection * selection = context.getSelection();
		selection->add(obj);
	}
	return;
}

bool
CmdLineAction::doList (ActionContext const & context) {
  bool hasActions = !_list.empty();
	if (!hasActions && _requestQuit) {
		sp_file_exit();
		return true;
	}

	for (std::list<CmdLineAction *>::iterator i = _list.begin();
			i != _list.end(); ++i) {
		CmdLineAction * entry = *i;
		entry->doIt(context);
		if (_requestQuit) {
			sp_file_exit();
			return true;
		}
	}
  return hasActions;
}

bool
CmdLineAction::idle (void) {
	std::list<SPDesktop *> desktops;
	INKSCAPE.get_all_desktops(desktops);

	// We're going to assume one desktop per document, because no one
	// should have had time to make more at this point.
	for (std::list<SPDesktop *>::iterator i = desktops.begin();
			i != desktops.end(); ++i) {
		SPDesktop * desktop = *i;
		//Inkscape::UI::View::View * view = dynamic_cast<Inkscape::UI::View::View *>(desktop);    
		doList(ActionContext(desktop));
	}
	return false;
}

} // Inkscape

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :