Saturday, June 2, 2012

How to make a website (Linux)

The internet is a hard subject to grasp if you do not understand networking. All it is is a system of servers connected by an internet service provider, which give you access to all of these servers. There is nothing special about a server, either. It is just a computer like the one you are using. You too can set up a website to use locally and over the internet. Here is how to do it in any variant of Gnu/Linux that has Apache and Kompozer (optional).

This article assumes that you have no knowledge of HTML (The Hypertext Markup Language), which is what websites are technically written in. Instead we will use a program called kompozer that is generates HTML similar to how blogger does.

Run these two commands to get started:

sudo apt-get install apache2
sudo apt-get install kompozer

The first command installs the server software and the second installs kompozer, the editor we will be using. After you install apache2, a directory named "www" will be created in /var. This is where you will place the file named "index.html".

You can link to other pages from the main one by making a link with the name of a file in /var/www or any other file on the computer.

Write the Webpage
 Run kompozer with root privileges. (sudo kompozer). You are going to be saving the file in /var/www which is a protected directory by default. Now, all you have to do is... write. Kompozer has all of the tools you will need to make your basic web page. Add text, links, bold text, italicized text, pictures, or whatever you need. If you need mroe power you can edit the HTML directly from the "source" tab if you must. When you are done writing your web page, save it.

The first dialog that will come up will ask you for a title. Name it whatever you want the name of this page to be. This will appear in tabs and on some window lists. Hit okay, and save it as "index.html" in /var/www.

Viewing
You now have a website available on your machine to the world wide web and to your local network. Find your ip address by typing

ifconfig

into the terminal. The wlan address is your IP address. Your local network can access the webpage from this address. To view it from anywhere in the world, you need to port forward your router. To do so, look up your router's manual on port forwarding. My router has an "advanced" dialog at 192.168.0.1, so that may be a good place to start looking. After the forwarding has been set up, you can go to http://your_public_ip to see your website. Typing "ip" into a google search will show you your public IP to use.

Open Source Development Model

Many people do not understand what the term "Free" (in the sense of libre) means to the fullest of its power when it relates to software. Many confuse it with free as in free beer, or "gratis" as Linux creator Linus Torvalds calls it. This is an in-depth comparison between the two terms and how it relates to software.

Common misconceptions
Whenever I bring up the subject of open-source software with somebody who does not write code, they always use the same argument. "So, what? You don't think that people should be rewarded for what they do?" In fact, this is why many people associate open-source supporters with the communist party. However, this is not the case. Projects where a lot of the code is open-source (such as OS X or Android) still generate tons of cash. Advertising also draws a huge monetary gain for open-source developers.  In all revisions of the GNU GPL selling free software is strongly encouraged, showing that it can and is done.

Simply releasing the code does not make a program free (in the definition of the Free Software Foundation). To be free it must be licensed under a license that stops it from becoming proprietary in the future. In my "travels" I met a man that was a fan of the BSD license, boasting that it was even more free than the GNU GPL. However, the project he was referencing was not original. Had it been a few years later that he decided to maintain the project, it may have already become depracted by a proprietary variant.

Unlimited Potential
The difference in the development models of proprietary and open source development is described in "The Cathedral and the Bazaar". They differ in one key area: who writes the code.

In a closed-source development model, a developer or team of developers work on one copy of a product and release it to the public. Only they can monitor the code and they are wholly responsible for fixing bugs and making the code more efficient. This costs man hours, and it is why a lot of payed programs are proprietary.

On the other hand, Open Source development happens in a much more community-esque fashion. An author or team of authors conceives a product and begin development, and either immediately or at a point where the barebones functionality has been met they release it to the public under a free license. Up to this point the style this happens is much like a closed-source model, but the magic of open source occurs at the next step. Any hobbyist or paid programmer (some companies actually pay developers to write free code) in the world (if it is hosted on the internet) can write bug fixes and new features for the program. They can also host their own variant of the program if they feel their addition changes the functionality or aesthetics too much.

The Downside
The weakness of the Open Source model is the same as its strength: anyone can see the source. Crackers, malaware authors, or anyone wishing to break a system can analyze the code, find the weaknesses, and write exploits for them. However, whenever a gap is released it is usually spotted and squashed within weeks.

Tuesday, May 29, 2012

Setting up and FTP server in Linux

This tutorial is for Gnu/Linux operating systems. Windows users can use the filezilla server: http://filezilla-project.org/download.php?type=server

A server is the name given to a piece of hardware running software that allows it to interact with client computers. All websites run on servers. This is a tutorial that uses a set of tools to configure a special type of server that uses FTP, the File Transfer Protocol. This is used for the transfer of files between computers. It is similar to what cloud storage services such as Dropbox or Skydrive offer.

The tools
To get the server running we will be using gadmin-proftpd. This is a GTK+ frontend to proftpd, the command line tool. Download it from sudo apt-get install gadmin-proftpd. Run it and enter the root password. The next few sections will describe each tab you need to fill in.

Servers
You need to fill in some of these blank fields. For Server Address use the inet addr from wlan of the ifconfig output in the terminal. The server name can be anything. Admin email should be your real email address. Skip down to where it says "signed certificate settings". You should fill this information in and use the "Apply" button to make a new certificate. Failure to complete this step of the setup process is what causes most errors with gadmin-proftpd.

Users
Users is the last tab you should have to fill out.  Fill in the username and password as you want them. Others will need to fill in this information, so make it reasonable. Go to the bottom and click "Add Directory". You should make a directory in /var/www called ftp (it can be called whatever you want it to be) that will act as this new user's directory. This will become the root ftp directory for that user (kind of like / for a normal user). If you want full access over a machine, make this "/".  Click "New User". This user will now be created.

Afterwards
 At the top of the application, click "Activate". The server will then be running. To access the server, go to your browser and type: "ftp://[your ip]" where [your ip] is you full ip address. To add directories and upload data you will need a program like bareftp, (sudo apt-get install bareftp) filezilla (which doesn't play nice with proftpd servers), or a windows program like coreftpd.

Extras
This is a local server only. You can make it point to the internet in your router settings. Your router setup (the ip address of your router, sometimes 192.168.0.1) should have some type of "advanced" dialog where you can set up your server. FTP runs on port 21, so you would need to fill in the protocol, port, and IP address wherever the dialog may ask for it. If it asks for a name like our D-link router does, fill it in with the name of your computer. Now, when you visit your external ip address (find this with "ip" on google) using the ftp protocol it will go to your server.

You can also use a no-ip.com host (which are free) to avoid the need to remember the ip address of the server.

Saturday, May 26, 2012

SCT - A custom language code translator

I have been hard at work working on a translator. At last, it has been complete. The translator is a very lightweight program that analyzes a file and a dictionary to translate them between user specified languages. This means that every user on the planet can have their own custom programming language and still be able to convert it into C++. Here is how it works:

 SCT - The translator
Specific Code Translator, SCT for short, analyzes two files. One is called a langdefines. The other is the code written in the language specified in the langdefines file. Here is how it works:

Every langdefines file has this syntax: old_new. So, if we wanted to specify the keyword "void" being "nothing" in a new language the syntax would be: void_nothing in the langdefines. Commenting and newlines are not yet supported in the langdefines file, but support for both are coming in future updates.Whitespace is readable.

Then, the translator will pick apart the langdefines and parse each individual side into two separate arrays. After setup, translation happens.

The translator will read a code file specified by the user as well as reading in the output file and a magichar. More about those two later, though.

The translator will replace new strings with old strings, thus converting it back into the language on the left. However, this is very specific, which is why Specific is the first word of the program. What may seem like design flaws are actually features to enable maximum usage of the translator.

The translator will replace EVERY string matching in the arrays. To avoid things like cout << "torch" from becoming cout << "t||rch" you have to use the sensitive strings feature. Sensitive strings in the code are not parsed, which gives them safety. Output, comments, variable names, etc. should all go in sensitive strings. You use a magichar to specify a sensitive string. The sensitive string goes in between two magichars specified at run time, like so: cout << "$torch$"
Neither torch nor the "$"s will appear in the code.

Advantages
You can use this program in a number of ways, including fixing massive spelling mistakes in documents, creating a custom syntax for a programming language, or for just showing off to your friends your new language. The way SCT works allows for "inline" coding, meaning that if you were to use it to add some pizazz to C++'s syntax you could still use C++ if you wanted to.

Future Plans
There are a few more plans in store for the program. I want to make reverse translation easier than messing with the code directly and I want to improve the syntax of the langdefines file.

The necessary files are all at my github. My custom langdefines is for a language I call NRSL. Some people that I have asked have Really hated the syntax, so go ahead and judge for yourself.

Thursday, May 10, 2012

C++ - Understanding Pointers

Pointers are a shockingly complex yet very simple thing to master in C++. The idea of a pointer is easy for most, yet the implementation can be a bit tricky for some.

Below is a quick example showcasing pointers. Comments are lingual representations of each line.

int x = 10; //Initialize integer x to 10.
int *px=&x; //Point variable px to the memory address (&) of x.

cout << px <<endl;/*Output the value of px, which is the numeral string representing the memory address of x.*/

cout << *px<<endl;/*Output the value of what px is pointing to.*/

int **ppx=&px;/*Initialize pointer ppx to point at the memory address of px, the pointer of x.*/

cout << *ppx<<endl;/*Output the contents of what ppx holds, which is the memory address of x.*/

cout << **ppx<<endl;/*Output the value of what the pointer this pointer points to is pointing at*/

**ppx+=1;/*The chain of pointers as displayed in the comment above happens and assigns variable x to 1. This is the same as *px=1; and x=1;*/

So, as you can see, pointers are a somewhat confusing concept. Your next question may be "Why even use them?" Well, the answer is: you shouldn't. Only use pointers in situations when they are required by the language or for a considerable performance gain, such as function parameters. This is called passing by reference since you give the reference to a variable's memory address rather than the value of the variable itself. This means that functions do not have to work with copies. You can make functions look like they take value rather than reference by doing something like this:

void doStuff(int &param){}  
This passes by reference because of the "&" operator, which returns the memory address of the parameter rather than the parameter itself. Now, the function can be called exactly like it were pass by value, like so:

doStuff(arg);  

 Pointers are not useless. They are extremely useful since you can have multiple pointers pointing to the same variable and even pointers pointing to other pointers. This comes in especially handy with multithreading.

Tuesday, May 1, 2012

Comparison of Media Players - Gnu/Linux

Media players are applications that handle the playing of audio and/or video. (In this comparison the listening of music will be the only factor, not how well/if these applications can play video.)
There is wide debate over which media player is superior, and this will list some of the pros and cons of each media player. Of course a lot of information is left out and of course there are a lot more than are listed, but this article will point a media player-hopper in the correct direction.

1. Clementine
 First off, I will state that Clementine is the application that I have used as of late for playing music. Playing music is what its specialty is. However, using Clementine to play a sound file would be overkill. It does a great job of saving your playlists for later listening and includes utilities for mass file renaming and organization.

Clementine can stream music from the internet and sync with devices. Common media player characteristics are included, such as shuffling and looping.

One of the selling points of Clementine is the ability to destroy the main window and have it reduce to the notification icon. From there you can control the volume, pause, play, move to next track, move to previous track, mute, love, ban, quit, or show the main window again. Clementine also integrates into the Ubuntu sound menu.

2. Amarok
  Amarok, the music player commonly paired with KDE due to the use of Qt, is the media player I had used before switching to Clementine. A very unique feature to Amarok that I had never seen before was the automatic fetching of information from wikipedia about the song and the fetching of lyrics. Of course, once the information was fetched it was cached and it does not rely on fetching to function properly. Amarok also integrates with your portable devices and will ask you before it begins to unlock them if they are locked, which can be troublesome if it misidentifies something to be a device.

Amarok is also capable of working without the existence of the main window. It will fall back to a notification icon with previous, pause, play, and next options as well as the option to show the main window.

3. VLC Media Player

VLC is a popular music player that is very good at what it does. It specializes in streaming but can also be used as a general purpose music player. If I have files that I do not want in my common playlist I use VLC since it does not save playlist information through usages. VLC does not excel with audio playback, but is rather used a video streaming/viewing application.

VLC, like Amarok and Clementine, has a notification icon but it cannot live without the main window. VLC has a very good equalizer

4. mplayer
 Mplayer has arguably the largest learning curve out of any of the media players. It does not come with a graphical frontend, so it must be run from the command line. It cannot remember playlists, you must make a playlist and start the application with the playlist as one of the arguments.

Mplayer has frontends available for it, like SMPlayer. Mplayer by itself does not work with the mouse, so you must memorize the keyboard commands to use it. Some find mplayer to be the best way to listen to audio since you can run it without a window or notification icon at all if you so please.

Interestingly enough, mplayer can play videos very well and is my video player of choice.

5. Rhythmbox
Rhythmbox is the default music player in Ubuntu 12.04. Banshee's absence has given Rhythmbox the opportunity to expand. Rhythmbox is now the official applications for the Ubuntu music store. Like Amarok and Clementine, Rhythmbox syncs to devices with arguably the most accuracy of the bunch. Rhythmbox does not have a notification icon and cannot live without keeping the main window open, so controlling the window may seem a bit bulky at first who are used to being able to kill the main window or control the flow of music through a notification icon.

6. DeadBeef
No, this media player is not built of mutilated cow. What it IS built with is C and GTK (I assume it is C, anyway.) The GTK dependency makes it lighter than a media player like Clementine that has Qt bindings. If you use a Gnome, XFCE, LXDE, *box, or Unity desktop environment; the download will only be about 5 MiB.

The Media player does not offer much in the way of customization or organization like Clementine, but it does get the job done like a media player should. It can import / export playlists, list statistics about the current playlist, and most importantly live without the main window and run as a daemon from the system tray. If you want to use Clementine without the elevated RAM usage and superfluous features, DeadBeef is the media player for you.
Honorable mentions
I did not include the following popular media players:

Banshee: Rhythmbox does nearly the same thing as Banshee.

XBMC: "VIM is a text editor; Emacs is an Operating System" is my favorite quote regarding text editors. XBMC is the Emacs of text editors. It is its very own ecosystem and would not be practical to use for multitasking. This is definitely the choice for a user wanting a heavily media-centric computing experience.

Conclusion (opinion)
I cannot call the conclusion fact since all of these media players do exactly what they are supposed to: play media. I just feel that Clementine has the best functionality. It can run in the background or have the main window; it doesn't matter to Clementine. It also does everything that Amarok can do like sync devices, fetch data, and live without a main window, so the presence of those features somewhat invalidates Amarok.

Bear in mind this opinion is only based in audio, and for video mplayer is my choice. I have no justification for this and VLC does just a good as job as mplayer.

Monday, April 30, 2012

Programming in Qt 2 - The core

Introduction
Signals
Slots
Exercise
 Function definition
 Connecting the slot
Finishing things up


Introduction
In the first tutorial I taught you how to basically create a "hello world" program in QT and change the value of a button. However, explicitly defining everything in that method is not the purpose of a GUI. User interaction is a key feature of any user interface, so in this tutorial I will show you how to create a button that toggles its text. To do so I will use signals and slots. Before we delve into the code, I will explain what those two things are.

Signals
During the execution of a Qt application there is a main loop until the user commands the program to stop or there is a serious exception. Signals are Qt constructs that are emitted when a certain action has occurred. You can create your own signals with the emit keyword, but for now we will only worry about custom slots.

Slots
Slots are the code executed when a signal has been emitted. These slots are connected to the signals of a certain object within the application. For instance, the slot we will make will be connected to the clicked() signal of the PushButton.

Exercise
 Function Definition
Create a toggling button
Open Qt creator and open the .pro file that you created in the last exercise. .pro files, or project files are what tell qmake to include that particular file in compilation.

You should already have an application complete with a PushButton in the center. If not, follow the steps in the first tutorial to get to that point. Our first step is to create a prototype for toggleVal() in MainWindow.h. Go to MainWindow.h in Qt creator. The file is located under the "headers" tab which may be collapsed.

I usually append the class after private: since that is the end of the generated code, but you can place these next two lines wherever you want. Some prefer to do it after public:.You need to add this code:

public slots:
void toggleVal();

As you probably already know from studying C++ this creates a prototype, or undefined declaration, of the function toggleVal(), which we will define in MainWindow.cpp.

Navigate to MainWindow.cpp and after the definition of the MainWindow deconstructor add the following lines:

void MainWindow::toggleVal(){
    QString val1="Value 1";
    QString val2="Value 2";
    if(ui->pushButton->text()=="Value 1")
        ui->pushButton->setText("Value 2");
    else if(ui->pushButton->text()=="Value 2")
        ui->pushButton->setText("Value 1");
} 

This is the slot for the PushButton. There are a few new things here. QString is used instead of std::string for Qt text. std::strings can be converted using the fromStdString() function of QString.

text() for pushButton returns the displayed text and setText() sets the text.

Why were the ifs so strict? I find it to be in good style to explicitly define my conditions in my graphical interfaces since other functions could possibly change the value and you may not want it changing at that point. You can define it however you wish, though. The main parts of this code to take are text() and setText().

 Connecting the slot

So far the only thing we have accomplished is making a function that checks the value of a button and changes the text accordingly. This can be called explicitly, but again, that does not give the user enough control. We will connect the slot in the MainWindow constructor of MainWindow.cpp.


After the code 
ui->setupUi(this);
add these two lines:
ui->pushButton->setText("Value 1"); QObject::connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(toggleVal()));






The first line explicitly sets the text of the button to "Value 1" so that the function can work properly. The second line is a bit trickier.

We call the connect function of QObject which has 4 parameters. The first is the sender, which in this case is the pushButton. Then, the signal  to act on. The signal is placed inside SIGNAL(). If you typed this yourself rather than copy-pasting, you would have seen the list of signals available for use with PushButton. Next is the member, which in this case is the MainWindow. Then, the slot. Like SIGNAL() slots must also go within SLOT(). We supplied our toggleVal() function, but you will see a list of predefined slots if you type that code yourself.

Multiple slots can be connected to a single signal, and multiple signals to a single slot. This becomes an important concept once you have multiple ways to quit or to initiate an action.

This power is immense. You can define toggleVal() to do anything you want. Close the application, parse, move the application wildly around the desktop, draw squares with OpenGL... Anything that Qt and C++ are capable of can be done in slots.

Finishing things up
In this tutorial you learned how to define and connect signals and slots. In the next few tutorials, you will learn about some other parts of the Qt library like QString and QPainter in depth. The next tutorial will be on a slight delay as I still have Qt related learning to do before I am able to write it. Other resources do an alright job of explaining things, but the purpose of these tutorials is to provide a very straightforward approach to Qt.