Create a QML plugin to run system commands? - c++

I'd like to run commands and show the output in quite a few Ubuntu Touch apps I'd like to create, so therefore I need to create a plugin for QML in C/C++(?) with this functionality.
The thing is that I don't know anything about C/C++, nor do I really want to learn it. However I do need it now and I need help to get going.
How would I create this plugin? Where do I start?
Thanks in advance,
Daniel

You can always wrap QProcess which allows you to run any shell command.
Check this out
And for Ubuntu touch you can use wizard for qml plugins. Its quite easy actually.

I got help from Joseph Mills: https://code.launchpad.net/~josephjamesmills/+junk/launchTotem

You shall use PlasmaCore.DataSource with the engine "executable", as in this example.
Mind that the call is asynchronous. Hence the command sends a signal when it finishes, and a different part of the code picks the signal and continues with the next step you want to perform.

Related

Can I run a command in another app using C++?

I'm not sure if anyone asked this, I tried to find it but I couldn't. I'm starting a new project and I want to make a GUI in C++ that will run another application called "GAMS" which does some optimization problems for me.
Now, I do know how to create GUI and how to run GAMS itself using ShellExecute(), but I don't know how to run those algorithms from my GUI.
All I need is to run scrip that I wrote in GAMS and that I would be able to manipulate with data that I receive from GAMS.
So my question is, can I send commands from my GUI to GAMS?
Thanks in advance!!

Qt Console with Bash

I am writing a program using the Qt framework. I would like the user to be able to have access to a console/terminal from within the application itself.
In other words, the user should be greeted with a "BASH" prompt when they start the program.
I have looked into QTermWidget, but there doesn't seem to be too much documentation on it and it doesn't seem to be up to date either.
I've looked at QConsole, but it only seems to be able to run TCL/Python consoles.
How would I be able to embed a terminal into my application?
There is really not much tutorial needed for QTermWidget, although there is one here.
The purpose of the widget is that it does not require any complication. The code would be something like this without the extra settings:
QTermWidget *console = new QTermWidget();
QMainWindow *mainWindow = new QMainWindow();
mainWindow->setCentralWidget(console);
It is also not necessarily true that it is not up-to-date. It was recently updated to build against Qt 5 properly. If you lack anything, please use the issue tracker on github.
It should be more or less in mature and "complete" state, that is why you may not see heavy progress. It is just a widget after all, not a big framework.
You could try QProcess. This is not strictly "embedding" a terminal in your app, but it it really easy to use, you can kick off a terminal that is owned by your app. You can even connect (with signals / slots) to its output and interact with it in a limited fashion... depends on what you need.
Here is the doc with some simple examples: http://qt-project.org/doc/qt-5/QProcess.html
I am not at my Qt-PC today so I don't have an "interacting" example for you, but if you think this is a way for you to go then I can dig that out...

C++, linux: how to limit function access to file system?

Our app is ran from SU or normal user. We have a library we have connected to our project. In that library there is a function we want to call. We have a folder called notRestricted in the directory where we run application from. We have created a new thread. We want to limit access of the thread to file system. What we want to do is simple - call that function but limit its access to write only to that folder (we prefer to let it read from anywhere app can read from).
Update:
So I see that there is no way to disable only one thread from all FS but one folder...
I read your propositions dear SO users and posted some kind of analog to this question here so in there thay gave us a link to sandbox with not a bad api, but I do not really know if it would work on anething but GentOS (but any way such script looks quite intresting in case of using Boost.Process command line to run it and than run desired ex-thread (which migrated to seprate application=)).
There isn't really any way you can prevent a single thread, because its in the same process space as you are, except for hacking methods like function hooking to detect any kind of file system access.
Perhaps you might like to rethink how you're implementing your application - having native untrusted code run as su isn't exactly a good idea. Perhaps use another process and communicate via. RPC, or use a interpreted language that you can check against at run time.
In my opinion, the best strategy would be:
Don't run this code in a different thread, but run it in a different process.
When you create this process (after the fork but before any call to execve), use chroot to change the root of the filesystem.
This will give you some good isolation... However doing so will make your code require root... Don't run the child process as root since root can trivially work around this.
Inject a replacement for open(2) that checks the arguments and returns -EACCES as appropriate.
This doesn't sound like the right thing to do. If you think about it, what you are trying to prevent is a problem well known to the computer games industry. The most common approach to deal with this problem is simply encoding or encrypting the data you don't want others to have access to, in such a way that only you know how to read/understand it.

Is there a 'restart' function in Windows/C++

In a windows project I am working on, I intend to have a menu selection that copletely restarts the app. Is there a Windows or C++ function that does this?
There isn't a built-in for this, but a well-designed application can simply stop everything that's going on and then loop back to the start. If you want a true 'fresh start', you will have to spawn a new process (possibly as the last thing you do before the old one shuts down.)
No, you must do it yourself.
For instance, you can run external process which will wait until you exit your application, and then run it again.
Actually you might want to take a look at the Restart Manager API that came in with Windows Vista. As ever you can p-invoke this to your hearts content and theirs explicit support coming for it in Visual C++ 2010.
Already needed to do this. The easiest way without any further reading would be to write a simple .bat-file (either by hand or dynamically by your application) starting your program and then calling that bat-file from your application.
The bat-file may even contain a line to remove itself after having started your app...
You want to call CreateProcess and then close your current instance of the application gracefully with ExitProcess(), or if you link to the C runtime, just return from main().
But first you should ask yourself why you need to recreate the process in the first place.
ExitWindowsEx is what you want. You can also run the shutdown.exe utility built into windows.
shutdown -t0 -r (restart the system after 0 seconds)

A terminal-like window for wxWidgets?

I'm looking to add an element to my wxWidgets GUI that behaves like a terminal emulator. Not in terms of a shell which executes commands, but just the input-output setup of an application running in a terminal.
Basically, the requirements are:
Streaming input/output: When you enter a character, it is added to an input stream, and when something is piped to the terminal, it prints out immediately.
No editing: Once you type in a character, it's permanently there, since it's probably been consumed by the application running in the terminal.
Some sort of scrolling (even if it just shows a few lines or something).
It would be nice if there is something that already does this, but suggestions on how to implement this with already existing controls such as wxTextCtrl would also be welcome.
I know this is a couple weeks late, but hopefully it's still useful. I've got a project called Chameleon that uses a wxWidgets-based VT100 terminal widget, which was itself based off of a project called taTelnet. The Chameleon source is available from my website (download page here). Not sure if it's exactly what you're looking for, but it might give you some ideas. Feel free to let me know if you have any questions about it.
wxWidgets supports redirecting STDOUT to a wxTextCtrl via wxStreamToTextRedirector. As for input, you could override the OnChar event in a wxTextCtrl-derived class to handle this.