I need to find if a printer has a given model.
I found QPrinterInfo - that has a property printerName
Unfortunately, when adding a printer in Linux the user can make up the name however he wants, or change it after install, so the name would not be reliable
I read that Qt 5.x has a "printerModel" and that would be great... but I have to use 4.7-8
I tried to read from /etc/cups/printers.conf
QString fileName = "/etc/cups/printers.conf";
QFile printerConf(fileName);
if (printerConf.open(QIODevice::ReadOnly | QIODevice::Text))
{
// i wish but it never happens
}
But I can't read it because of permissions.
Qt 4.8, Ubuntu
The cups API has all the information needed:
http://www.cups.org/documentation.php/api-cups.html
#include <cups/cups.h>
cups_dest_t *dests;
int num_dests = cupsGetDests(&dests);
cups_dest_t *dest;
int i;
const char *value;
for (i = num_dests, dest = dests; i > 0; i --, dest ++)
if (dest->instance == NULL)
{
value = cupsGetOption("printer-make-and-model", dest->num_options, dest->options);
printf("%s (%s)\n", dest->name, value ? value : "unknown");
}
cupsFreeDests(num_dests, dests);
Related
i am currently trying to figure out a way to write a file (an allegro configuration file to be exact) to a mounted zip-file using physfs and allegro 5.
reading the config file works fine, but when it comes to writing the changed config, nothing happens (e.g. the file is not re-written and thus remains in it's old state).
also, when not using physfs, everything works perfectly.
here's the code i use:
Game::Game(int height, int width, int newDifficulty)
{
PHYSFS_init(NULL);
if (!PHYSFS_addToSearchPath("Data.zip", 1)) {
// error handling
}
al_set_physfs_file_interface();
cfg = al_load_config_file("cfg.cfg");
if (cfg != NULL) // file exists, read from it
{
const char *score = al_get_config_value(cfg, "", "highScore");
highScore = atoi(score); // copy value
}
else // file does not exist, create it and init highScore to 0
{
cfg = al_create_config();
al_set_config_value(cfg, "", "highScore", "0");
highScore = 0;
al_save_config_file("cfg.cfg", cfg);
}
...
}
and in another function:
void Game::resetGame()
{
// high score
if (player->getScore() > highScore)
{
highScore = player->getScore();
// convert new highScore to char* that can be saved
stringstream strs;
strs << highScore;
string temp_str = strs.str();
char const* pchar = temp_str.c_str();
if (cfg != NULL) // file exists, read from it
{
al_set_config_value(cfg, "", "highScore", pchar);
al_save_config_file("cfg.cfg", cfg);
}
}
...
}
since the code works without physfs, i guess i handle the config file itself correctly.
any help would be highly appreciated!
cheers,
hannes
in the meantime, i solved the issue myself.
apparently, physfs has no ability to write to an archive.
therefore, i need to PHYSFS_setWriteDir("jhdsaf"), save the cfg-file in that folder and then replace the original zip-file by an updated version with the cfg-file, just before the game closes (after all resources are unloaded because the zip is otherwise still in use).
if anyone is interested in the code to do this, just reply to this post!
hannes
So i am trying to create a Command Line Interpretor in Ubuntu using C/C++
It is part of our lab task for college
The basic feature of the CLI is to take input from the user , parse it to find the command and its arguments , then look up the pathname for the command from the Environment Variable
I have been able to parse the String , get the command and its arguments , I have also been able to read to the Path Environment Variables to get the directories of all the paths. Now i must look through these directories to find where the file (command) lies , and then return the complete path so it can be sent to execve for execution in the child process
i have to create a lookup function which takes the arguments array (the 0th index position contains the command name ) and the array of directories
Here is an outline of the function that has been provided to us
// Search the directories identified by the dir argument to see
// if the argv[0] (the filename) appears there. Allocate a new
// string, place the full path name in it, then return the string.
//
char* lookupPath(char **argv, char **dir){
char* result;
char pName[MAX_PATH_LEN];
// check if is already an absolute path name
if( *argv[0] == '/' ) .....
// look in PATH directories, use access() to see if the
// file is in the dir
for( i = 0 ; i < MAX_PATHS ; i++ ) .....
// File name not found in any path variable
fprintf(stderr, "%s: command not found\n", argv[0]);
return NULL;
}
here argv is the array of arguments (0th index contains the command and the following index contains arguments)
and dir[] contains an array of all the directories
Now how em i suppose to traverse trough the directories to find the full path for the give command ?
This more than I probably should have provided but there are enough things left you need to figure out that it isn't a slam dunk.
#include <unistd.h>
typedef std::vector<std::string> pathArray;
std::string lookupPath(const std::string pgm, pathArray &paths)
{
int ret;
for (int i = 0; i < paths.size(); ++i)
{
std::string temp = paths[i];
temp = temp + "/" + pgm;
// let execv determine if it is executable
// or you can do that here if required
if ((ret = access(temp.c_str(), F_OK)) == 0)
return temp;
}
return("");
}
int main(int argc, char *argv[])
{
pathArray pa;
pa.push_back("/bin");
pa.push_back("/usr/bin"); //and the rest of PATH contents
std::string pgm;
std::string fullpath;
pgm = "ls";
if ((fullpath = lookupPath(pgm, pa)) != "")
{
//execv(fullpath.c_str(), ....);
//etc...
}
else
{ //could not find pgm
}
return(0);
}
The following code works in a the Marmalade simulator (I'm on OSX using x-code)
bool PictureDictionary::OnTableSelect(CTable* table, int tab){
//if something is selected, look up the item, and display it
//also change the search to the selected item
if(-1 < tab){
// if a term is selected, set the search text field to the term
CString term = m_SearchResults.GetString(tab);
if(m_currentWord != (char*)term.Get()){
m_currentWord = (char *)term.Get();
m_searchTextField->SetAttribute("text", term);
char* normalizedTerm = (char *)term.Get();
char* imagePath;
sprintf(imagePath,"images/%s.jpg", normalizedTerm);
if(m_ImageAttached){
m_Image->SetAttribute("image", (const char*)imagePath);
} else {
m_Image = CreateImage(CAttributes()
.Set("name", "picture")
.Set("x1", "0")
.Set("x2", "0")
.Set("y1", "50%")
.Set("image", (const char*)imagePath)
);
m_SearchView->AddChild(m_Image);
m_ImageAttached = true;
}
}
}
return true;
}
When I run the simulator, and select an item from the table, the image appears, and changes when I select a different item. When I go to refactor, I get a EXC_BAD_ACCESS (code=1…..) Error
bool PictureDictionary::OnTableSelect(CTable* table, int tab){
//if something is selected, look up the item, and display it
//also change the search to the selected item
if(-1 < tab){
// if a term is selected, set the search text field to the term
CString term = m_SearchResults.GetString(tab);
if(m_currentWord != (char*)term.Get()){
m_currentWord = (char *)term.Get();
m_searchTextField->SetAttribute("text", term);
char* normalizedTerm = (char *)term.Get();
char* imagePath;
sprintf(imagePath,"images/%s.jpg", normalizedTerm);
UpdatePictureView(imagePath);
}
}
return true;
}
void PictureDictionary::UpdatePictureView(char* imagePath){
if(m_ImageAttached){
m_Image->SetAttribute("image", (const char*)imagePath);
} else {
m_Image = CreateImage(CAttributes()
.Set("name", "picture")
.Set("x1", "0")
.Set("x2", "0")
.Set("y1", "50%")
.Set("image", (const char*)imagePath)
);
m_SearchView->AddChild(m_Image);
m_ImageAttached = true;
}
}
Any suggestions on how to clean up the code without getting these issues?
Edit RE Comments about uninitialized variables:
m_ImageAttached was initialized to false in the constructor, unless I'm doing something wrong. Also, changing the condition to check if m_Image!=NULL also throws the same error.
main.cpp:
PictureDictionary pictDict(myApp, &dictionary);
Constructor for PictureDictionary:
PictureDictionary::PictureDictionary(CAppPtr app,Dictionary::Dictionary* dictionary){
m_App = app;
m_Dictionary = dictionary;
m_currentWord = "";
m_ImageAttached = false;
}
imagePath is an unitialized pointer, in both snippets. Any attempt to dereference is undefined behaviour. It just appeared to work in the first snippet. Use an array or populate a std::string instead:
std::string imagePath(std::string("images/") + normalizedTerm + ".jpg");
And use std::string::c_str() if access to the underlying const char* is required.
I hear that fontconfig is the best option for getting fonts in linux. Unfortunately, I've been looking through their developer documentation and I have absolutely no clue what I'm doing. It would appear there is no simple function to get a list of system fonts. I have to perform a pattern search instead... right?
In short, what is the best way to get a list of true-type fonts (their family, face, and directory) with fontconfig? Of course, if there's something better than fontconfig, I'm certainly open to other solutions.
I had a similar question, and found this post (the fontconfig documentation is a little difficult to get through). MindaugasJ's response was useful, but watch out for the extra lines calling things like FcPatternPrint() or printing out the results of FcNameUnparse(). In addition, you need to add a FC_FILE argument to the list of arguments passed to FcObjectSetBuild. Something like this:
FcConfig* config = FcInitLoadConfigAndFonts();
FcPattern* pat = FcPatternCreate();
FcObjectSet* os = FcObjectSetBuild (FC_FAMILY, FC_STYLE, FC_LANG, FC_FILE, (char *) 0);
FcFontSet* fs = FcFontList(config, pat, os);
printf("Total matching fonts: %d\n", fs->nfont);
for (int i=0; fs && i < fs->nfont; ++i) {
FcPattern* font = fs->fonts[i];
FcChar8 *file, *style, *family;
if (FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch &&
FcPatternGetString(font, FC_FAMILY, 0, &family) == FcResultMatch &&
FcPatternGetString(font, FC_STYLE, 0, &style) == FcResultMatch)
{
printf("Filename: %s (family %s, style %s)\n", file, family, style);
}
}
if (fs) FcFontSetDestroy(fs);
I had a slightly different problem to solve in that I needed to find the font file to pass to freetype's FC_New_Face() function given some font "name". This code is able to use fontconfig to find the best file to match a name:
FcConfig* config = FcInitLoadConfigAndFonts();
// configure the search pattern,
// assume "name" is a std::string with the desired font name in it
FcPattern* pat = FcNameParse((const FcChar8*)(name.c_str()));
FcConfigSubstitute(config, pat, FcMatchPattern);
FcDefaultSubstitute(pat);
// find the font
FcResult res;
FcPattern* font = FcFontMatch(config, pat, &res);
if (font)
{
FcChar8* file = NULL;
if (FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch)
{
// save the file to another std::string
fontFile = (char*)file;
}
FcPatternDestroy(font);
}
FcPatternDestroy(pat);
This is not exactly what you are asking for, but it will give you the list of fonts available.
#include <fontconfig.h>
FcPattern *pat;
FcFontSet *fs;
FcObjectSet *os;
FcChar8 *s, *file;
FcConfig *config;
FcBool result;
int i;
result = FcInit();
config = FcConfigGetCurrent();
FcConfigSetRescanInterval(config, 0);
// show the fonts (debugging)
pat = FcPatternCreate();
os = FcObjectSetBuild (FC_FAMILY, FC_STYLE, FC_LANG, (char *) 0);
fs = FcFontList(config, pat, os);
printf("Total fonts: %d", fs->nfont);
for (i=0; fs && i < fs->nfont; i++) {
FcPattern *font = fs->fonts[i];//FcFontSetFont(fs, i);
FcPatternPrint(font);
s = FcNameUnparse(font);
if (FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch) {
printf("Filename: %s", file);
}
printf("Font: %s", s);
free(s);
}
if (fs) FcFontSetDestroy(fs);
since 4 days, I try to see how XLib works, and I have finally understood that.
Si I tried to make a short program wich retrieve open window's name.
For that, I created 2 functions :
Window *list (Display *disp, unsigned long *len) {
Atom prop = XInternAtom(disp,"_NET_CLIENT_LIST",False), type;
int form;
unsigned long remain;
unsigned char *list;
if (XGetWindowProperty(disp,XDefaultRootWindow(disp),prop,0,1024,False,XA_WINDOW,
&type,&form,len,&remain,&list) != Success) {
return 0;
}
return (Window*)list;
}
So, this first function return a window object of all the windows.
Then, I created a function to retrieve the name from all those windows.
char *name (Display *disp, Window win) {
Atom prop = XInternAtom(disp,"WM_NAME",False), type;
int form;
unsigned long remain, len;
unsigned char *list;
if (XGetWindowProperty(disp,win,prop,0,1024,False,XA_STRING,
&type,&form,&len,&remain,&list) != Success) {
return NULL;
}
return (char*)list;
}
And this function works fine, an main.c example:
int main(int argc, char* argv[]){
int i;
unsigned long len;
XKeyEvent esend;
Display *disp = XOpenDisplay(NULL);
Window *list;
char *name;
list = (Window*)list(disp,&len);
for (i=0;i<(int)len;i++) {
name = name(disp,list[i]);
printf("%d : %s \n",i,name);
free(name);
}
}
And, It works really fine, except for Skype windows it returns:
1 : Xlib Programming Manual: Keyboard and Pointer Events - Google Chrome
2 : Debian Web [En fonction] - Oracle VM VirtualBox
3 : XChat: necromoine # / (+CSTfnst 10:2)
4 :
5 : root#root-Laptop: ~
6 :
And, the number 4 and 6 are blank (I actually have two opened skype window).
Can you help me please.
A WM_NAME is not necessarily a simple string. It could be a compound text (a different type of string), which is actually the case for Skype windows. You need to use AnyPropertyType instead of XA_STRING to get the property, then format according to actual type. Look at the source of xprops to see how it's done.