Recent Posts

Pages: [1] 2 3 ... 10
1
Applications/Code Snippets / Re: Computer "WakeOnTime" class
« Last post by SteveDee on February 21, 2012, 04:50:14 PM »
My guess would be that you have not created the clsAlarmClock class properly. I don't know your level of ability so forgive me starting with the basics.

When you create a new project to try this code example, right click on the "Classes" node in the left hand pane and select new > Class...

In the New File dialog enter the new class Name as: clsAlarmClock

After clicking "OK" you should have a new file with " 'Gambas class file" at the top. Now just paste the class code into this file.

If you still have a problem, you may need to post all your code (for clsAlarmClock.class & FMain.class) here.

If your project does run, start by clicking Button3 (the "Read" button) which should display the current contents of your BIOS (probably 1970-01-01 and 00:00:00).

Good luck.
2
Applications/Code Snippets / Re: Computer "WakeOnTime" class
« Last post by Folkwang on February 19, 2012, 10:59:11 AM »
Hello SteveDee,

I put all of these lines in Gambas2.
 When I run the prog then Gambas2 says:
"Unknown identifier: clsAlarmClock in line 3 in FMain.class".

What is wrong ??
3
Applications/Code Snippets / Giving Your Programs A Voice
« Last post by SteveDee on February 05, 2012, 04:07:01 AM »
I've found it very helpful in a few cases to add audible alarms to monitoring functions. For example, if you work in a network support environment and a server goes down, you need know about it a.s.a.p.
By using text-to-speech you can monitor several aspects of your working environment without having to continually view screens.

I recommend eSpeak which is a compact open source software speech synthesiser. Once installed, you can test it by typing something in a terminal:-
Code: [Select]
espeak hello
...or...
Code: [Select]
espeak "hello steve"

You can change the default voice by using the voice switch, so:-
Code: [Select]
espeak -ven+f4 "hello steve"
...should give you an English female voice.

Having different voices can be useful. For example, you could use a female voice when a server goes down, and a male voice when a new job request arrives on you Help Desk. With the monitoring system that we use, I've created 10 voices which are simply selected at random.

Using eSpeak With Gambas.
This is really easy:-

Code: [Select]
strMessage="hello steve"
EXEC ["espeak", strMessage]

...or with more control:-

Code: [Select]
strMessage="Hello, My name is Monica"
's=rate, p=pitch, v=voice, f=female, k=emphasis on capitals
EXEC ["espeak", "-s130", "-p70", "-ven+f4", "-g5", "-k5", strMessage]

So here is a simple Gambas speech alarm routine:-
Code: [Select]
PUBLIC SUB Alarm(strMessage AS String,strVoice AS String)

  SELECT CASE strVoice
    CASE "monica"
    strVoice="-ven+f4"
    CASE "bill"
    strVoice="-ven+m4"
    ...
    {more voices}
    ...
    CASE ELSE
    strVoice="-ven+m1"
  END SELECT

  's=rate, p=pitch, v=voice, f=female, k=emphasis on capitals
  EXEC ["espeak", "-s130", "-p70", strVoice, "-g5", "-k5", strMessage]
  WAIT 2 {you may need a delay depending upon repeat period & length of text}
END


Have Fun!
4
Applications/Code Snippets / Simple app to add menu items on Lubuntu
« Last post by SteveDee on February 04, 2012, 10:08:56 AM »
Lubuntu is a lightweight version of Ubuntu and does not include a menu editor. This appears to exercise many users on the Ubuntu forum, so here is a beginners project which simply allows menu items (apps and other shortcuts) to be added to the existing menu structure.

Menu items automatically appear in the Lubuntu menu structure when a .desktop file is added to: /usr/share/applications/

The essential elements of these files appear to be:-
[Desktop Entry]
Encoding=UTF-8
Type=Application
Name={name of application}
Exec={command line}
Comment={a mouse-over type tooltip}
Categories={a category name}

...and here is a simplified example;
[Desktop Entry]
Encoding=UTF-8
Type=Application
Name=Calculator
Exec=galculator
Comment=Its a calculator
Categories=Utility;

So all this simple program will do is allow the user to enter 3 parameters; Name, Command & Comment, and to select the destination menu.

One other point is that we need root priviliges to write the new .desktop file to: /usr/share/applications

To do this we launch this program using gksu (i.e. modify the launcher command for this application to: gksu /usr/bin/Launcher4Lulu.gambas).

This application needs the following components on the main form:-
Labels   (4)   to label the textboxes and listbox
Textbox   (3)   txtName, txtCommand, txtComment
Listbox   (1)   lstCategories
Checkbox (1)   chkTest
Button   (1)   btnSave

The code:-
Code: [Select]
' Gambas class file
'=======================================================
'Launcher4Lulu
'-------------
'This simple app can create menu entries to
' launch applications for Lubuntu.
'As it needs to write to /usr/share/applications
' it must be launched using gksu
' So after creating the .deb and installing, modify
' the Launcher4Lulu properties to include gksu
'i.e. Command: gksu /usr/bin/Launcher4Lulu.gambas
'
'steve davis
'Jan 2012
'========================================================
CONST FILE_HEAD AS String = "[Desktop Entry]"
CONST ENCODING AS String = "Encoding=UTF-8"
CONST TYPE AS String = "Type=Application"

PUBLIC SUB btnSave_Click()
DIM strName AS String     'this is both the menu name and file name
DIM strCommand AS String  'the command to launch your target app
DIM strComment AS String 
DIM strCategories AS String 'used to determine menu position
DIM strFileContent AS String  'contains complete .desktop file content
DIM hFile AS File
DIM strFilePath AS String     'tafget path will be /usr/share/applications/

  IF txtName.Text <> "" AND txtCommand.Text <> "" AND lstMenu.Text <> "" THEN
    'at least the important fields are not empty...
    strName = "Name=" & txtName.Text
    strCommand = "Exec=" & txtCommand.Text
    strComment = "Comment=" & txtComment.Text
    strCategories = lstMenu.Text
    'menu names may differ from Categories
    SELECT CASE lstMenu.Text
      CASE "Accessories"
      strCategories = "Categories=Utility;"
      CASE "Graphics"
      strCategories = "Categories=Graphics;"
      CASE "Internet"
      strCategories = "Categories=Network;"
      CASE "Office"
      strCategories = "Categories=Office;"
      CASE "Programming"
      strCategories = "Categories=Development;"
      CASE "Sound & Video"
      strCategories = "Categories=AudioVideo;"
      CASE "System Tools"
      strCategories = "Categories=System;"
      CASE "Preferences"
      strCategories = "Categories=Settings;"
      CASE ELSE
      strCategories = "Categories=Utility;"
    END SELECT
    ' Now build the file contents...
    strFileContent = FILE_HEAD & Chr(10) & ENCODING & Chr(10) & TYPE
    strFileContent = strFileContent & Chr(10) & strName & Chr(10) & strCommand
    strFileContent = strFileContent & Chr(10) & strComment & Chr(10) & strCategories & Chr(10)
   
    '***this block can be removed or commented out when testing is complete...
    Message.Info(strFileContent) '<<<this message line should be deleted after testing
    IF chkTest.Value THEN
      'test program without root priviliges
      strFilePath = "/home/steve/Desktop/"
    ELSE
      'will need root priviliges to write here...
      strFilePath = "/usr/share/applications/"
    ENDIF
    '***test block ends
   
    hFile = OPEN strFilePath & strName & ".desktop" FOR CREATE
    WRITE #hFile, strFileContent, Len(strFileContent)
    CLOSE #hFile   
  ELSE
    Message.Info("Please enter required data", "close")
  ENDIF
CATCH
  Message.Error("Ooops! We have a problem!")
END

Any application that has been added to the menu structure can then also be added to the Desktop or the Panel. So if you wanted a launcher in the Panel to load the BBC website, you would first create a menu entry with a command line like: firefox www.bbc.co.uk

This application could be extended to load existing .desktop files and move them within the menus by simply editing the Categories parameter.
5
Programming / Loading a jpg file into an image
« Last post by johnaaronrose on January 17, 2012, 12:32:56 PM »
I've been looking at someone else's code from a 2007 posting. I'm using Gambas 3.0 with SQLite 3.6.22. I've done a similar piece of coding to that person to get the image from a file.

I get a popup window containing 'Null object' at runtime (which unfortunately does not point at problem line of code, though I think that it must be 'image = Image.Load(Dialog.Path)') with following code, when I click ButtonImportImage:

Public Sub ButtonImportImage_Click()
  Dim image As Image
  Inc Application.Busy
  Dialog.Filter = ImageFileFilter(True)
  Dialog.Title = "Import Image"
  ' load image
  If Dialog.OpenFile() Then Return
  Print "import image path=", Dialog.Path
  image = Image.Load(Dialog.Path)
  ' save image & thumb to temp files
  SaveImageAndThumb(image)
  ButtonExportImage.Enabled = True
Finally
  Dec Application.Busy
Catch
  Message.Warning(ERROR.Text)
End

On execution, TempImagePath is valid and points to a .jpg file which loads with Graphics apps such as gThumbThumbViewer. I'm baffled as to the cause. Any ideas?

Grasping at straws: I'm using Gambas3 with SQLite 3.6.22. I just noticed that Gambas3 has a new component gb.image.io - image loading & saving. If I try and select it (in Project Properties) it seems to be incompatible with gb.gui, gb.gtk & gb.qt4. Perhaps it's needed for the Image.Load line - I need to load an Image for the SaveImageAndThumb procedure (which is working when called by another Sub).
6
Programming / Re: How to set foreign key in SQLite
« Last post by johnaaronrose on January 16, 2012, 11:47:18 AM »
There no support for Foreign Keys in gb.db & gb.sqlite3 components: so the method that I previously tried is not available.

I didn't realise that a table could be created using the Exec() method. I've done that (including the Foreign Key clause) and it worked! Works in Gambas2 & Gambas3.

PS Unfortunately, sqlite3 does not allow "ALTER TABLE ADD CONSTRAINT" to add the foreign key: I've tried it & the statement was rejected with a syntax error. Further confirmation of this can be found on:http://stackoverflow.com/questions/1884818/how-do-i-add-a-foreign-key-to-an-existing-sqlite-3-6-21-table
7
Programming / How to set foreign key in SQLite
« Last post by johnaaronrose on January 10, 2012, 10:10:34 AM »
I've looked at the Database examples in Gambas2. However, I don't see how to set a field as a foreign key when creating a table. I'm able to set a field as a table's primary key using:
JHTable.PrimaryKey = ["_id"]
after:
JHTable = databaseConnection.Tables.Add("country")
JHTable.Fields.Add("_id", db.Serial)

I expected after keying in "JHTable." that ForeignKey would come up as an option for selection but it didn't.

Any ideas please?
8
Programming / Re: i have difficulties in understanding
« Last post by johnaaronrose on January 10, 2012, 04:25:38 AM »
I'm using Gambas2. When I started it I noticed the 'Open Example' menu entry which had the 'Picture Database' example in it. I'd forgotten about this in Gambas. I've been too involved with Android of late!
9
Programming / Re: Search and replace in file
« Last post by smil3y on January 09, 2012, 02:40:45 PM »
Ok, I'll answer my own question  :P

Replacing specific string in file is very simple:
Code: [Select]
DIM content as String

' read the content of the file
content = File.Load("/path/to/file.ext")

'replace the string "old" with "new" (without quotes) in the content of the file
content = Replace$(content, "old", "new")

' save the new content to file
File.Save("/path/to/file.ext",content)
This can be 2-lines code but I prefered to explain how it's done (or at least the way I figured out)

However, if you don't know the exact word you want to replace (you want to search for pattern match) things get complicated, here is the function I use now:
 
Code: [Select]
PUBLIC FUNCTION Replace_Str(SrchFile AS String, SrchStr AS String, RpcStr AS String)
  DIM content_array AS String[]
  DIM sLine, content AS String
  DIM counter AS Integer
 
  ' load file content
  content = File.Load(SrchFile)
  ' split the content of the file to array with new line as delimiter
  content_array = Split(content, "\n")
 
  counter = 0
  ' read file line by line
  FOR EACH sLine IN content_array
    ' match pattern search agains SrchStr
    IF sLine LIKE SrchStr & "*" THEN
      ' save file content if SrchStr found and replace
      File.Save(SrchFile, Replace$(content, sLine, RpcStr))
     
      ' stop here, no need to search more (for me. to replace globaly remove BREAK)
      BREAK
    ENDIF
     
    counter += 1
  NEXT
 
' if the last lines was reached and still there wasn't match pop-up error message (remove if replacing globaly)
  IF counter = content_array.Count THEN
    Message.Error("Unable to find:\n\nString: " & SrchStr & "\n\n In: " & SrchFile)
  ENDIF
END

I hope this helps other Gambas users :)

More about pattern syntax : http://gambasdoc.org/help/doc/pcre

Cheers!

EDIT: If replacing globaly then exclude the error message too (noted in code)
10
Programming / Re: i have difficulties in understanding
« Last post by johnaaronrose on January 09, 2012, 11:20:21 AM »
I saw your message below.It referred to the PictureDatabase example in Gambas 2. Could you point me to it's URL?


You can store Pictures in a database in Gambas 2. Have a look at the PictureDatabase example in Gambas 2. This is for SQLite. But the notes in the code show how to swap it to MySQL and PostgreSQL.

Also have a look at the topic http://forum.stormweb.no/index.php/topic,305.0.html
Pages: [1] 2 3 ... 10